Why doesn't the compiler report an error when a variable not declared as mutable is modified?

前端 未结 2 1423
后悔当初
后悔当初 2020-12-20 17:56

I installed Rust 1.13 and tried:

fn main() {
    let x: u32;
    x = 10; // no error?
}

When I compiled this file there\'s some warnings, b

2条回答
  •  鱼传尺愫
    2020-12-20 18:22

    What you have written is identical to:

    let x: u32 = 10;
    

    The compiler will not permit you to mutate it thereafter:

    let x: u32;
    x = 10;
    x = 0; // Error: re-assignment of immutable variable `x`
    

    Note that it is a compiler error if you try to use an uninitialized variable:

    let x: u32;
    println!("{}", x); // Error: use of possibly uninitialized variable: `x`
    

    This feature can be pretty useful if you want to initialize the variable differently based on runtime conditions. A naive example:

    let x: u32;
    if condition {
        x = 1;   
    } else if other_condition {
        x = 10;
    } else {
        x = 100;
    }
    

    But still it will still be an error if there is a possibility that it isn't initialized:

    let x: u32;
    if condition {
        x = 1;   
    } else if other_condition {
        x = 10;
    } // no else
    println!("{:?}", x); // Error: use of possibly uninitialized variable: `x`
    

提交回复
热议问题