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?
alice
2
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
system
Closed
4
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.