What happens to ownership when a variable is passed to a function in Rust?
When a variable is passed to a function, ownership of the variable is moved to the function unless the variable implements the Copy trait or is passed by reference.
Click to reveal answer
beginner
How can you allow a function to use a variable without taking ownership in Rust?
You can pass a reference to the variable using the & symbol, so the function borrows the variable instead of taking ownership.
Click to reveal answer
intermediate
What is the difference between passing by value and passing by reference in Rust functions?
Passing by value moves ownership to the function, potentially invalidating the original variable. Passing by reference lets the function borrow the variable without taking ownership, so the original variable remains valid.
Click to reveal answer
intermediate
What happens if a function returns a value that it took ownership of?
The ownership of the returned value is transferred back to the caller, allowing the caller to use the value again.
Click to reveal answer
beginner
Why does Rust prevent use of a variable after its ownership has been moved to a function?
Rust enforces ownership rules to prevent bugs like use-after-free. Once ownership is moved, the original variable is invalidated to ensure memory safety.
Click to reveal answer
What keyword allows a function to borrow a variable without taking ownership in Rust?
Amut
B*
C&
Dref
✗ Incorrect
The & symbol is used to pass a reference, allowing borrowing without transferring ownership.
If a function takes ownership of a String, what happens to the original variable after the function call?
AIt is borrowed mutably.
BIt becomes invalid and cannot be used.
CIt is copied automatically.
DIt remains valid and unchanged.
✗ Incorrect
Ownership moves to the function, so the original variable is invalidated and cannot be used.
How can a function return ownership of a value back to the caller?
ABy using the Copy trait.
BBy passing a reference.
CBy cloning the value.
DBy returning the value from the function.
✗ Incorrect
Returning the value transfers ownership back to the caller.
Which of these types is copied rather than moved when passed to a function?
Ai32
BString
CVec<T>
DBox<T>
✗ Incorrect
Primitive types like i32 implement the Copy trait and are copied instead of moved.
What does Rust prevent by enforcing ownership rules in function calls?
AUse-after-free bugs
BSyntax errors
CInfinite loops
DType mismatches
✗ Incorrect
Ownership rules prevent use-after-free bugs by ensuring variables are not used after ownership moves.
Explain how ownership is transferred when passing variables to functions in Rust and how to avoid moving ownership.
Think about how & references work compared to normal variable passing.
You got /4 concepts.
Describe what happens when a function returns a value it owns and how ownership is managed in that case.
Consider how ownership moves back to the caller after the function finishes.
You got /3 concepts.
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.