Is the valuation order taken into account when analyzing ownership in an expression?

In Rust 1.0, does the compiler consider the evaluation order when analyzing ownership in an expression? For instance:

struct Foo { val:usize }

fn main() {
    let mut v:Vec<Foo> = Vec::with_capacity(1);
    v.push(Foo{val:0});

    let f = Foo{val:0};
    v[f.val] = f; // error: use of moved value
}

It could not be compiled because the index is in the array f.val, but fmoves to the array. I do not understand why using f.valas an index on LHS may contradict the transfer of ownership of f to RHS.

+4
source share
2 answers

This type of thing is not defined at the language level, although obviously the compiler has to do something.

+1
source

Clone Copy , , , ( ) .

#[derive(Clone, Copy)]
struct Foo { val: usize }

Copy, , :

let index = f.val; // usize is copyable, so no moves here
v[index] = f;

.

+1

All Articles