nrc / r4cppp

Rust for C++ programmers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

unused_mut warning in control-flow.md

LudwikJaniuk opened this issue · comments

This code:

fn foo(x: i32) -> &'static str {
    let mut result: &'static str;
    if x < 10 {
        result = "less than 10";
    } else {
        result = "10 or more";
    }
    return result;
}

Gives a warning that the mut on result is not used and recommends removing it. It made me play around with it and notice that rust is very smart about statically analyzing when things are initialized. I think it would have been a great time to mention that.