Ownership and Borrowing
Ownership
Moving a String
Each heap value has one owner. Assigning a String moves ownership, so the original name can no longer be used.
Program
Play the program to watch s1 move into s2.
ownership_move.rs
fn main() {
let s1 = String::from("hello");
let s2 = s1;
let len = s2.len();
println!("{s2} {len}");
}
ownership
A `String` owns heap memory through a single binding.
move
`let s2 = s1` moves ownership; `s1` is no longer usable.
len
`s2.len()` returns the byte length of the string.