Why can't you assign to multiple variables at once?


#1

Why isn’t it possible to assign values to two variables at once even though you can define them at the same time?

For example, this works:

let (mut prev, mut curr) = (3i32, 5i32);
println!("{}, {}", prev, curr);

But this does not:

let (mut prev, mut curr) = (3i32, 5i32);

(prev, curr) = (curr, prev + curr);

println!("{}, {}", prev, curr);

Shouldn’t there be a nice way to give both of these variables new values at the same time since they depend on each other? In Python the swap works as expected, and it makes a lot of code easier to read/write.


#2

This has been suggested before; I opened an issue for it here.


#3

Okay, cool. (I searched for “swap variables,” not “destructuring assignment,” which is why I couldn’t find it.)

I hope this feature gains traction. It’s one of the (many) reasons Python code looks so elegant.