Rust Core
Ownership and borrowing
Rust associates each value with an owner. When ownership moves, the previous binding can no longer use the value unless the type implements Copy or a borrow is created.
rust
fn length(value: &String) -> usize {
value.len()
}
fn main() {
let name = String::from("LaySoftware");
println!("{}", length(&name));
println!("{}", name);
}Mental model
Ask: who owns this value, how long must it live, and can I borrow it immutably or mutably at this point?
Lesson complete?Use the next lesson to continue the learning path.