Is there a difference between partially moved values ​​and moved values ​​in Rust?

Currently in Rust master (0.10-pre), when you move one element of a unique vector and try to move another element, the compiler complains:

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

This error message is slightly different than if you were supposed to move the entire vector:

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

Why another message? If xonly “partially moved” in the first example, is there a way to “partially move” different parts x? If not, why not just say it's xmoved?

+4
source share
1 answer

x " ", " " x?

, , :

let x = ~[~1, ~2, ~3];
match x {
    [x1, x2, x3] => println!("{} {} {}", *x1, *x2, *x3),
    _ => unreachable!()
}

, xN , :

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

- .

+1

Source: https://habr.com/ru/post/1523794/


All Articles