I have never experimented with languages like C, C++, Go, etc., and I decided to start with Rust, I already understand a little what the stack and the Heap are, but, what does it really mean by moving a variable, the documentation says that it is not a shallow copy:
... probably sounds like making a shallow copy. But because Rust also invalidates the first variable, instead of calling it a shallow copy, it’s known as a move. In this example, we would say that
s1
was moved intos2
...
For example:
let s1 = String::from("hello");
let s2 = s1;
println!("{}, world!", s1);
What does the documentation mean when it says "invalidates". Does this mean that Rust invalidates s1
and assigns the value that was in s1
to s2
, so... s1
doesn't exist? o Does it have any value?, that's mainly what I don't understand, does it really move it? or is there still any value in s1
in memory?
From what I could understand, this check happens at compile time, so it makes me think that s1
literally doesn't exist in memory and only s2
, since s1 was literally moved to s2
.
Obviously this happens with values that have an unknown size, that is, in the heap.
I hope you can help me understand. :)
cargo run
hahaha ), practically the variables1
is not initialized, that is, it is not given a value in memory, since the value is initialized ins2
, that is, it "transforms" it or understands it like this:let s1; let s2 = String::from("hello"); println!("{}, world!", s2);
. so there is no reference, shallow copies ins1
, am I understanding correctly? hahaha. – Jurkoic