Confusion with ownership

Why does the following code work:

fn main() {
    let a = String::from("apple");
    let b = &a;
    let c = b;

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

I would expect the compiler to throw an error, because c should take ownership of the value contained in b, and so b should no longer be valid?

Is there such a thing as "ownership of a borrow" i.e. can a variable own a borrow?

This is because all immutable references implement the Copy trait, so anything that would otherwise be a move just duplicates the value.

As for ownership of a borrow, yes you can talk about that. References are values like any other value and ownership applies to all values.

4 Likes

Thanks a lot!