I wrote the line of codes below:
fn main() {
let mut s = vec!["my".to_string(), "here".to_string(), "babe".to_string()];
let t = s;
s = vec!["three".to_string()];
let u = s;
}
based on my understanding of rust compiler behavior and the rules of rust ownership, after I moved the ownership of the value of s (which is not a primitive type and doesn't implement COPY trait cause it's type of Vec) to t in line 2, I should not be able to change the value of s cause this variable (I mean s) is not valid anymore! but this lines of code are valid at all. Can anyone explain why ?
I expect that to face an compiler error by run this code!
You are correct about you cannot read
swhile some scope becausevec moved intotand vector is notCopy. However, in Rust, you are able to read from (or write to)sagain once if you reassign some vector to it (yes, it also can be moved out to somewhere).Technically it is equivalent with following code, and works fine: