I have tried the following snipets in Python
l1 = [0,1]
l2 = [0,1]
a, b = 0, 1
(l1[a], l1[b], l2[l1[a]], l2[l1[b]]) = (l1[b], l1[a], a, b)
print (l1)
print (l2)
The result is:
[1, 0]
[1, 0]
However this is what I expected: First, a,b is plugged in the whole expression as
(l1[0], l1[1], l2[0], l2[1]) = (l1[1], l1[0], 0, 1)
Then finally it will print:
[1, 0]
[0, 1]
Same on Rust,
fn main() {
let mut l1 = [0,1];
let mut l2 = [0,1];
let (a, b) = (0, 1);
(l1[a], l1[b], l2[l1[a]], l2[l1[b]]) = (l1[b], l1[a], a, b);
println!("{:?}", l1);
println!("{:?}", l2);
}
prints
[1, 0]
[1, 0]
My guess on this behavior is: only the right expression is evaluated
(l1[a], l1[b], l2[l1[a]], l2[l1[b]]) = (1, 0, 0, 1)
then the assignments are done serially:
l1[a] = 1
l1[b] = 0
l2[l1[a]] = 0 #l2[1] = 0
l2[l1[b]] = 1 #l2[0] = 1
Why is this happened?