Beginner question: destructuring assignment and mutability

I just started learning Rust. The following code runs without error. May someone please explain why let (x,y) instead of let (mut x, mut y) is the correct code? I get a compiler warning saying mut x and mut y don't need to be mutable if I use the let (mut x, mut y) version.

fn main() {
    let (x, y);
    (x, ..) = (3, 4);
    [.., y] = [1, 2];
    assert_eq!([x, y], [3, 2]);
    println!("Hello, world!");
}

Mutable is only required if you want to change the value. If you don't set any value in the beginning, then the first time you initialize it there is no previous value so mut is not required.

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.