Problems with mut variables


#1

Hi, i wanted to execute the following code snippet:

fn main() {
let x = 5;
let (mut x,y) = (1,2);
println!("{}",x);
println!("{}",y);

}

and i got the following error:

warning: unused variable: x
–> main.rs:2:9
|
2 | let mut x = 5;
| ^^^^^
|
= note: #[warn(unused_variables)] on by default
= note: to disable this warning, consider using _x instead

warning: variable does not need to be mutable
–> main.rs:2:9
|
2 | let mut x = 5;
| ^^^^^
|
= note: #[warn(unused_mut)] on by default

So does this mean that, that x with the prefix ‘mut’ is a new variable?


#2

Yes it is a new variable. It is better to define it as mutable from the start


#3

Currently in Rust you can’t update the contents of variables using the tuple/slice syntax:

#![feature(slice_patterns)]
fn main() {
    let mut x = 0;
    let mut y = 0;
    (x, y) = (10, 20);
    [x, y] = [30, 40];
}

Hopefully this ergonomy limitation will be lifted, eventually.