3 ms·
Indeed, the behavior described here is just a consequence of allowing pattern-matching when assigning variables. Say you wanted to do the Python trick of swappi
by kibwen 13y ago
Indeed, the behavior described here is just a consequence of allowing pattern-matching when assigning variables. Say you wanted to do the Python trick of swapping two values:
let a = 1;
let b = 9;
let (b, a) = (a, b);
printf!("a: %i, b: %i", a, b); // a: 9, b: 1
...or say you just wanted to grab a single item out of a tuple:
let x = (1, 2);
let (_, y) = x; // the underscore is the pattern for "ignore this"
printf!("y: %i", y); // y: 2
- copx 13y agoJust for comparison: b, a = a, b is actually valid code in Lua (and works as expected). Grabbing only one value looks like this _, y = returnsTwoValues() -- grab only the second y = returnsTwoValues() -- grab only the first
- jfarmer 13y agoThis works similarly in Ruby: a,_ = two_values() _,b = two_values()