Namara

Code daily. Without assist.

Today's code

ownership.rs

fn main() {
    let s1 = String::from("namara");
    let s2 = s1;

    println!("{}", s1);
}

Does this compile? If not, why?

Answer

No. String doesn't implement Copy, so let s2 = s1; moves ownership out of s1. Using s1 again on the next line is a use of a moved value, and the compiler rejects it: error[E0382]: borrow of moved value: `s1`.