#Move question

6 messages · Page 1 of 1 (latest)

steel saddle
#

Noob question, but why does the first example move the value in the vector but the second doesn't?

First:

    let mut v = Vec::new();

    for i in 101..106 {
        v.push(i.to_string());
    }

    let third = v[2]; // moves
    let fifth = v[4]; // moves

Second:

    let v2 = vec![1, 2, 3];

    let second = v2[1]; // doesn't move
tight galleon
#

String isn't Copy, so the first example attempts to move the values, but i32 is Copy, so the second example just copies the values instead of moving them.

steel saddle
#

oh it's because it's not a string it's a type of &str

tight galleon
#

Yep, immutable references like &str are Copy

steel saddle
#

Interesting