Ownership and strings



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


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}");
}

examples/strings/give-ownership/src/main.rs
fn main() {
    let name = give_ownership();
    println!("{name}");
}

fn give_ownership() -> String {
    String::from("Foo")
}