← Back to Rust Course | Chapter 5: Ownership | Lesson 6 of 6

Ownership and Functions

When you hand a value to a function, you're often giving it away for good, unless you ask for it back.

Passing Ownership Into a Function

When a non-Copy value like a String is passed by value into a function, ownership moves into that function's parameter, and the caller can no longer use the original variable afterward.

Example: Passing Ownership Into a Function

markup
fn consume(s: String) {
    println!("Consumed: {}", s);
}

fn main() {
    let text = String::from("gift");
    consume(text);
}

Returning Ownership

A function can transfer ownership of a value back to its caller simply by returning it as the final expression, letting the caller keep using the data under a new binding.

Example: Returning Ownership

markup
fn create_greeting() -> String {
    String::from("Hello from a function")
}

fn main() {
    let greeting = create_greeting();
    println!("{}", greeting);
}

Passing Ownership In and Returning It Back Out

A function can accept ownership of a value, use it, and then return it again, effectively "borrowing" it for the duration of the call through moves rather than references.

Example: Passing Ownership In and Returning It Back Out

markup
fn add_exclamation(mut s: String) -> String {
    s.push('!');
    s
}

fn main() {
    let phrase = String::from("Rust is great");
    let phrase = add_exclamation(phrase);
    println!("{}", phrase);
}

Avoiding Unnecessary Moves with References

Instead of moving ownership every time data needs to be read, a function can accept a reference (covered in the next chapter), letting the caller keep using its original variable after the call.

Example: Avoiding Unnecessary Moves with References

markup
fn print_length(s: &String) {
    println!("Length is {}", s.len());
}

fn main() {
    let word = String::from("borrowed");
    print_length(&word);
    println!("Still usable: {}", word);
}
Common Mistakes
  1. Passing a String into a function by value and then trying to use it again in the caller afterward.
  2. Not realizing that returning a value from a function transfers ownership back out to the caller.
  3. Overusing owned parameters (String instead of &str) in functions that only need to read data temporarily.
Chapter Summary
  • Passing a non-Copy value into a function by value moves ownership into that function.
  • A function can return ownership of a value, transferring it back to the caller.
  • Tuples can be used to return multiple values, including handing back a value alongside a computed result.
  • Accepting a reference instead of an owned value lets a function use data temporarily without taking ownership.
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.