4 ms·
You can destructure without moving, as you suggest, by taking a reference. Move: let x = (box 2u, box 4u); let (y, z) = match x { (first, seco
by kwantam 12y ago
You can destructure without moving, as you suggest, by taking a reference.
Move:
let x = (box 2u, box 4u);
let (y, z) = match x {
(first, second) => (first, second)
};
println!("x is {}", x);
// this will not compile: x is moved into y and z
Reference:
let x = (box 2u, box 4u);
let (y, z) = match x {
(ref first, ref second) => (first, second)
};
println!("x is {}", x);
// this works fine
- dbaupp 12y ago(The `match` is unnecessary in both examples: the patterns are irrefutable and so work directly in `let`.)