2014-01-26 30 views
4

目前鏽病主(0.10前),當你移動一個獨特的向量的一個元素,並嘗試移動不同的元素,編譯器會抱怨:在Rust中部分移動的值和移動的值是否有區別?

let x = ~[~1, ~2, ~3]; 
let z0 = x[0]; 
let z1 = x[1]; // error: use of partially moved value: `x` 

此錯誤消息是,如果你是有所不同移動整個矢量:

let y = ~[~1, ~2, ~3]; 
let y1 = y; 
let y2 = y; // error: use of moved value `y` 

爲什麼不同的信息?如果x在第一個示例中僅爲「部分移動」,是否有任何方法「部分移動」x的不同部分?如果沒有,爲什麼不只是說x被移動?

+0

即使無法部分移動元素,但移動元素與移動整個矢量的方式並不相同,但仍然有不同的錯誤消息。它從來沒有傷害有準確的錯誤信息。 –

回答

1

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 

這對結構和枚舉也是如此 - 它們也可以部分移動。