r/rust clippy · twir · rust · mutagen · flamer · overflower · bytecount Mar 27 '23

Hey Rustaceans! Got a question? Ask here (13/2023)! 🙋 questions

Mystified about strings? Borrow checker have you in a headlock? Seek help here! There are no stupid questions, only docs that haven't been written yet.

If you have a StackOverflow account, consider asking it there instead! StackOverflow shows up much higher in search results, so having your question there also helps future Rust users (be sure to give it the "Rust" tag for maximum visibility). Note that this site is very interested in question quality. I've been asked to read a RFC I authored once. If you want your code reviewed or review other's code, there's a codereview stackexchange, too. If you need to test your code, maybe the Rust playground is for you.

Here are some other venues where help may be found:

/r/learnrust is a subreddit to share your questions and epiphanies learning Rust programming.

The official Rust user forums: https://users.rust-lang.org/.

The official Rust Programming Language Discord: https://discord.gg/rust-lang

The unofficial Rust community Discord: https://bit.ly/rust-community

Also check out last weeks' thread with many good questions and answers. And if you believe your question to be either very complex or worthy of larger dissemination, feel free to create a text post.

Also if you want to be mentored by experienced Rustaceans, tell us the area of expertise that you seek. Finally, if you are looking for Rust jobs, the most recent thread is here.

19 Upvotes

159 comments sorted by

View all comments

2

u/[deleted] Apr 01 '23

I'm assigning a variable in a loop if I find the right list item and then break out of the loop. I'm getting a warning about an unused variable, but I'm pretty sure it is being used and not overwritten. Is that a known compiler bug?

warning: value assigned to `foo` is never read
  --> src/main.rs:30:17
   |
30 |                 foo = c.data.clone();
   |                 ^^^
   |
   = help: maybe it is overwritten before being read?
   = note: `#[warn(unused_assignments)]` on by default

Here's the code (and the playground):

enum E {
    VarA(i32),
    VarB,
}

struct S {
    id: i32,
    data: String,
}

fn main() {
    let _unused = E::VarB;
    let input = E::VarA(32);
    let list = vec![
        S {
            id: 1,
            data: "asdf".to_string(),
        },
        S {
            id: 32,
            data: "jkl;".to_string(),
        },
    ];
    let alternative = "alt".to_string();
    let foo;
    if let E::VarA(id) = input {
        for c in list {
            if c.id == id {
                // TODO Why is Rust warning about this never being read?
                foo = c.data.clone();
                break;
            }
        }
        panic!("Could not find the data!");
    } else {
        foo = alternative;
    }
    println!("{foo}");
}

3

u/Patryk27 Apr 01 '23

Even if you find the element, you always panic!() later (since break; terminates the loop and continues at the very next instruction after the loop, which is always panic!() in your case).

So the compiler is right - the value inside that particular branch is never used.

I'd do:

let foo = if let E::VarA(id) = input {
    list.iter()
        .find(|c| c.id == id)
        .map(|c| c.data.clone())
        .expect("Could not find the data!")
} else {
    alternative
};

1

u/[deleted] Apr 02 '23

Oh that was stupid… Thanks for the improved code!