Back to questions
JuniorTheoryCommon
What happens when this program is built and run?
fn main() {
let s1 = String::from("hi");
let s2 = s1;
println!("{s1} {s2}");
}It does not compile
- Expecting
hi hi:Stringis notCopy, solet s2 = s1;hands ownership tos2and leavess1unusable; keeping both takeslet s2 = s1.clone();. - Expecting a panic at run time: the compiler tracks every move and stops the build with
error[E0382]: borrow of moved value: `s1`.
- How would you change the program so that it prints
hi hi? - Why does the same code compile when
s1holds ani32instead of aString?