How to Return Ownership from a Function in Rust

Return ownership in Rust by making the variable the final expression of a function without a semicolon to move the value to the caller.

To return ownership from a function in Rust, define the function to return the type (e.g., String) and place the variable as the final expression without a semicolon. This moves the value out of the function scope and into the caller.

fn gives_ownership() -> String {
    let some_string = String::from("yours");
    some_string
}

fn takes_and_gives_back(a_string: String) -> String {
    a_string
}

fn main() {
    let s1 = gives_ownership();
    let s2 = String::from("hello");
    let s3 = takes_and_gives_back(s2);
}