Borrowing means letting a function or variable use a value without taking ownership of it. It allows safe access without copying or moving the data.
Click to reveal answer
beginner
What are the two types of borrowing in Rust?
There are two types: immutable borrowing (read-only access) and mutable borrowing (read and write access).
Click to reveal answer
intermediate
Why can you have many immutable borrows but only one mutable borrow at a time?
To prevent data races and ensure safety, Rust allows multiple readers or one writer, but not both at the same time.
Click to reveal answer
intermediate
What happens if you try to have a mutable borrow while immutable borrows exist?
Rust will give a compile-time error because it could cause unexpected changes while others are reading the data.
Click to reveal answer
advanced
Explain the lifetime of a borrow in Rust.
A borrow's lifetime is the scope where the reference is valid. Rust checks lifetimes to make sure references don't outlive the data they point to.
Click to reveal answer
Which of the following is true about immutable borrows in Rust?
AYou can have only one immutable borrow at a time.
BYou can have multiple immutable borrows at the same time.
CImmutable borrows allow modifying the data.
DImmutable borrows transfer ownership.
✗ Incorrect
Immutable borrows allow multiple readers but no writers.
What error will Rust give if you try to have a mutable borrow while an immutable borrow exists?
ARuntime error
BNo error, it is allowed
CCompile-time error
DWarning only
✗ Incorrect
Rust enforces borrowing rules at compile time to prevent unsafe access.
What does borrowing prevent in Rust?
AData races and invalid memory access
BAll function calls
CVariable declaration
DProgram compilation
✗ Incorrect
Borrowing rules ensure safe access to data without conflicts.
Which keyword is used to create a mutable borrow in Rust?
A&mut
B&
Cmut
D*
✗ Incorrect
The &mut symbol creates a mutable reference (borrow).
What is the lifetime of a borrow?
AThe ownership duration
BThe size of the data
CThe number of borrows
DThe scope where the reference is valid
✗ Incorrect
Lifetimes ensure references do not outlive the data they point to.
Explain Rust's borrowing rules and why they are important for safe programming.
Think about how Rust prevents bugs by controlling who can access data and when.
You got /5 concepts.
Describe what happens if you try to have a mutable borrow while immutable borrows exist in Rust.
Rust stops you early to keep your program safe.
You got /4 concepts.
Practice
(1/5)
1. What does borrowing in Rust allow you to do?
easy
A. Copy data to a new variable
B. Use data without taking ownership
C. Delete data immediately
D. Create multiple owners of data
Solution
Step 1: Understand borrowing concept
Borrowing means using data without taking ownership, so the original owner keeps control.
Step 2: Compare options
Copying or deleting data changes ownership or data state, which borrowing does not do.
Final Answer:
Use data without taking ownership -> Option B
Quick Check:
Borrowing = Use without ownership [OK]
Hint: Borrowing means using data without owning it [OK]
Common Mistakes:
Confusing borrowing with copying
Thinking borrowing deletes data
Assuming borrowing creates multiple owners
2. Which of the following is the correct syntax to create an immutable borrow of a variable x in Rust?
easy
A. let y = &x;
B. let y = *x;
C. let y = &mut x;
D. let y = x;
Solution
Step 1: Identify immutable borrow syntax
Immutable borrow uses &x to create a reference without allowing changes.
Step 2: Check other options
&mut x is mutable borrow, *x dereferences, and x moves ownership.
Final Answer:
let y = &x; -> Option A
Quick Check:
Immutable borrow = &x [OK]
Hint: Immutable borrow uses & before variable [OK]
Common Mistakes:
Using &mut for immutable borrow
Dereferencing instead of borrowing
Assigning ownership instead of reference
3. What will be the output of this Rust code?
let mut s = String::from("hello");
let r1 = &s;
let r2 = &s;
println!("{} and {}", r1, r2);
let r3 = &mut s;
println!("{}", r3);
medium
A. hello and hello\nerror at runtime
B. hello and hello\nhello
C. Compile error due to mutable borrow while immutable borrows exist
D. hello\nhello and hello
Solution
Step 1: Analyze immutable borrows
Two immutable borrows r1 and r2 are allowed simultaneously.
Step 2: Check mutable borrow after immutable borrows
Trying to create mutable borrow r3 while immutable borrows exist causes compile error.
Final Answer:
Compile error due to mutable borrow while immutable borrows exist -> Option C
Quick Check:
Mutable borrow disallowed with active immutable borrows [OK]
Hint: No mutable borrow allowed if immutable borrows exist [OK]
Common Mistakes:
Assuming mutable borrow works after immutable borrows
Expecting runtime error instead of compile error
Confusing output with allowed borrows
4. Identify the error in this Rust code snippet:
let mut data = vec![1, 2, 3];
let r1 = &mut data;
let r2 = &data;
r1.push(4);
println!("{:?}", r2);
medium
A. No error, code runs fine
B. Cannot push to vector inside mutable borrow
C. Cannot print vector with {:?}
D. Cannot borrow data as immutable while mutable borrow exists
Solution
Step 1: Check borrows
Variable r1 is a mutable borrow, r2 is an immutable borrow of the same data.
Step 2: Understand borrowing rules
Rust forbids immutable borrow while mutable borrow is active, causing a compile error.
Final Answer:
Cannot borrow data as immutable while mutable borrow exists -> Option D
Quick Check:
Immutable borrow disallowed during mutable borrow [OK]
Hint: No immutable borrow allowed during mutable borrow [OK]
Common Mistakes:
Thinking push is invalid inside mutable borrow
Ignoring borrow conflict between r1 and r2
Assuming printing with {:?} causes error
5. You want to write a function that takes a mutable reference to a vector and appends a number, but also returns the length of the vector after appending. Which function signature and usage follow Rust's borrowing rules correctly?
hard
A. fn add_and_len(v: &mut Vec<i32>, n: i32) -> usize { v.push(n); v.len() }
let mut nums = vec![1,2];
let length = add_and_len(&mut nums, 3);
B. fn add_and_len(v: &Vec<i32>, n: i32) -> usize { v.push(n); v.len() }
let mut nums = vec![1,2];
let length = add_and_len(&nums, 3);
C. fn add_and_len(v: Vec<i32>, n: i32) -> usize { v.push(n); v.len() }
let nums = vec![1,2];
let length = add_and_len(nums, 3);
D. fn add_and_len(v: &mut Vec<i32>, n: i32) -> usize { v.len(); v.push(n) }
let mut nums = vec![1,2];
let length = add_and_len(&mut nums, 3);
Solution
Step 1: Check function signature for mutable borrow
Function needs &mut Vec<i32> to modify vector by pushing a number.
Step 2: Verify function body and usage
fn add_and_len(v: &mut Vec<i32>, n: i32) -> usize { v.push(n); v.len() }
let mut nums = vec![1,2];
let length = add_and_len(&mut nums, 3); pushes number then returns length correctly; usage passes mutable reference.
Step 3: Analyze other options
fn add_and_len(v: &Vec<i32>, n: i32) -> usize { v.push(n); v.len() }
let mut nums = vec![1,2];
let length = add_and_len(&nums, 3); uses immutable borrow but tries to push (not allowed). fn add_and_len(v: Vec<i32>, n: i32) -> usize { v.push(n); v.len() }
let nums = vec![1,2];
let length = add_and_len(nums, 3); takes ownership, not borrow. fn add_and_len(v: &mut Vec<i32>, n: i32) -> usize { v.len(); v.push(n) }
let mut nums = vec![1,2];
let length = add_and_len(&mut nums, 3); returns length before push, so length is old value.
Final Answer:
fn add_and_len(v: &mut Vec<i32>, n: i32) -> usize { v.push(n); v.len() }
let mut nums = vec![1,2];
let length = add_and_len(&mut nums, 3); -> Option A
Quick Check:
Mutable borrow needed to modify vector [OK]
Hint: Use &mut to modify data inside function [OK]
Common Mistakes:
Using immutable borrow when modifying data
Taking ownership instead of borrowing
Returning length before modification
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.