Ownership and strings
- take ownership
examples/strings/take-ownership/src/main.rs
fn main() { let name = String::from("Foo"); println!("{name}"); take_ownership(name); //println!("{name}"); // take_ownership moved the owner } fn take_ownership(name: String) { println!("in function: {name}"); }
Foo in function: Foo
- borrow
examples/strings/borrow/src/main.rs
fn main() { let name = String::from("Foo"); println!("{name}"); borrow(&name); println!("{name}"); } fn borrow(name: &str) { println!("in function: {name}"); }
- give ownership
examples/strings/give-ownership/src/main.rs
fn main() { let name = give_ownership(); println!("{name}"); } fn give_ownership() -> String { String::from("Foo") }