如果在第一个示例中 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
这也适用于结构和枚举——它们也可以部分移动。