If x is only "partially moved" in the first example, is there any way to "partially move" different parts of 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
這對結構和枚舉也是如此 - 它們也可以部分移動。
即使無法部分移動元素,但移動元素與移動整個矢量的方式並不相同,但仍然有不同的錯誤消息。它從來沒有傷害有準確的錯誤信息。 –