最初の例で x が「部分的に移動」しただけの場合、x のさまざまな部分を「部分的に移動」する方法はありますか?
はい、ありますが、これらのパーツを一度に移動した場合のみです。
let x = ~[~1, ~2, ~3];
match x {
[x1, x2, x3] => println!("{} {} {}", *x1, *x2, *x3),
_ => unreachable!()
}
xN
一致の後に追加の行を追加すると、 s が実際に移動されていることが簡単にわかります。
println!("{:?}", x);
コンパイラはエラーをスローします:
main3.rs:16:22: 16:23 error: use of partially moved value: `x`
main3.rs:16 println!("{:?}", x);
^
note: in expansion of format_args!
<std-macros>:195:27: 195:81 note: expansion site
<std-macros>:194:5: 196:6 note: in expansion of println!
main3.rs:16:5: 16:25 note: expansion site
main3.rs:13:10: 13:12 note: `(*x)[]` moved here because it has type `~int`, which is moved by default (use `ref` to override)
main3.rs:13 [x1, x2, x3] => println!("{} {} {}", *x1, *x2, *x3),
^~
error: aborting due to previous error
これは、構造体と列挙型にも当てはまります。部分的に移動することもできます。