📖 Scenario: Imagine you have a small program that manages a book title. You want to see how ownership works when you pass the book title to a function and get it back.
🎯 Goal: You will create a string variable for a book title, write a function that takes ownership of this string and returns it, and then print the returned title.
📋 What You'll Learn
Create a variable called book_title with the value "Rust Programming"
Write a function called take_and_return that takes ownership of a String parameter called title and returns it
Call the function take_and_return with book_title and store the result in a variable called returned_title
Print the value of returned_title
💡 Why This Matters
🌍 Real World
Understanding ownership is key in Rust to manage memory safely without a garbage collector.
💼 Career
Rust developers must master ownership rules to write efficient and safe code, especially when working with functions and data passing.
Progress0 / 4 steps
1
Create the initial book title
Create a variable called book_title and set it to the string "Rust Programming" using String::from.
Rust
Hint
Use let book_title = String::from("Rust Programming"); to create the string.
2
Write the function to take and return ownership
Write a function called take_and_return that takes ownership of a String parameter named title and returns it.
Rust
Hint
The function should accept title: String and return it directly.
3
Call the function and store the returned value
Call the function take_and_return with book_title and store the result in a variable called returned_title.
Rust
Hint
Use let returned_title = take_and_return(book_title); to call the function and save the result.
4
Print the returned book title
Print the value of returned_title using println!.
Rust
Hint
Use println!("{}", returned_title); to display the string.
Practice
(1/5)
1. What happens when a function in Rust takes ownership of a variable passed to it?
easy
A. The function borrows the variable temporarily.
B. The original variable can no longer be used after the function call.
C. The variable is copied and both original and function have ownership.
D. The variable is automatically cloned inside the function.
Solution
Step 1: Understand ownership transfer
When a function takes ownership, the original variable's ownership moves to the function.
Step 2: Effect on original variable
After the function call, the original variable is no longer valid and cannot be used.
Final Answer:
The original variable can no longer be used after the function call. -> Option B
Quick Check:
Ownership moves = original invalid [OK]
Hint: Ownership moves to function, original variable becomes unusable [OK]
Common Mistakes:
Thinking ownership is copied, not moved
Confusing borrowing with ownership transfer
Assuming automatic cloning happens
2. Which of the following function signatures correctly borrows a string slice without taking ownership?
easy
A. fn print_name(name: &str) { println!("{}", name); }
B. fn print_name(name: &String) { println!("{}", name); }
C. fn print_name(name: String) { println!("{}", name); }
D. fn print_name(name: String) -> String { name }
Solution
Step 1: Identify borrowing syntax
Borrowing a string slice uses &str type, which is a reference to string data.