Mutable references let you change data without taking ownership. This helps share data safely and avoid copying.
Mutable references in Rust
&mut variable_name
A mutable reference uses &mut before the variable name.
You can only have one mutable reference to a value at a time to keep data safe.
y to the mutable variable x.let mut x = 5;
let y = &mut x;fn add_one(num: &mut i32) {
*num += 1;
}
let mut value = 10;
add_one(&mut value);let mut s = String::from("hello"); let r = &mut s; r.push_str(", world");
This program shows how a mutable reference can change the original variable. We print the value before and after modifying it through the reference.
fn main() {
let mut number = 10;
println!("Before: {}", number);
// Create a mutable reference to number
let num_ref = &mut number;
*num_ref += 5; // Change the value through the reference
println!("After: {}", number);
}You cannot have more than one mutable reference to the same data at the same time.
Mutable references help avoid bugs by controlling who can change data.
Use * to access or change the value a reference points to.
Mutable references let you change data without moving ownership.
Only one mutable reference is allowed at a time to keep data safe.
Use &mut to create mutable references and * to access or modify the value.
Practice
&mut allow you to do in Rust?Solution
Step 1: Understand what
The&mutmeans&mutsymbol creates a mutable reference, which means you can change the value it points to without taking ownership.Step 2: Compare with other options
Options B and D describe immutable references or no changes allowed, and C talks about ownership transfer, which is different from mutable references.Final Answer:
Change the value it points to without taking ownership -> Option DQuick Check:
Mutable reference = change value without ownership [OK]
- Confusing mutable references with ownership transfer
- Thinking multiple mutable references are allowed simultaneously
- Believing mutable references prevent changes
num in Rust?Solution
Step 1: Identify the syntax for mutable references
In Rust, to create a mutable reference, you use&mutbefore the variable name.Step 2: Check each option
A creates an immutable reference, B dereferences the value, and D has incorrect syntax order.Final Answer:
let ref = &mut num; -> Option BQuick Check:
Mutable reference syntax = &mut variable [OK]
- Forgetting the 'mut' keyword after &
- Placing 'mut' before & incorrectly
- Using * instead of &mut for references
fn main() {
let mut x = 5;
let r = &mut x;
*r += 1;
println!("{}", x);
}Solution
Step 1: Understand the mutable reference usage
The variablexis mutable. A mutable referenceris created tox. The value pointed byris incremented by 1 using*r += 1;.Step 2: Determine the final value of
Sincexrpoints tox, incrementing*rchangesxfrom 5 to 6. The print statement outputs 6.Final Answer:
6 -> Option AQuick Check:
Mutable reference modifies original value = 6 [OK]
- Expecting original value unchanged
- Confusing reference with copy
- Thinking code causes compile error
fn main() {
let mut val = 10;
let r1 = &mut val;
let r2 = &mut val;
*r1 += 1;
*r2 += 1;
println!("{}", val);
}Solution
Step 1: Identify the mutable references
The code creates two mutable referencesr1andr2to the same variablevalat the same time.Step 2: Recall Rust's borrowing rules
Rust allows only one mutable reference at a time to prevent data races. Having two mutable references simultaneously causes a compile-time error.Final Answer:
Cannot have two mutable references to the same variable at the same time -> Option CQuick Check:
Only one mutable reference allowed at once [OK]
- Trying to create multiple mutable references simultaneously
- Thinking mutable references can be copied freely
- Ignoring Rust's borrowing rules
data using mutable references without causing a compile error?fn main() {
let mut data = 100;
let r1 = &data;
let r2 = &mut data;
*r2 += 50;
println!("{}", data);
}Solution
Step 1: Understand Rust's borrowing rules
Rust does not allow mutable and immutable references to the same variable at the same time. Here,r1is immutable andr2is mutable, causing a conflict.Step 2: Fix the code by removing the immutable reference
Removingr1allows only the mutable referencer2to exist, so modifyingdatathroughr2is safe and allowed.Final Answer:
Remove the immutable referencer1before creatingr2-> Option AQuick Check:
Mutable and immutable refs cannot coexist [OK]
- Trying to use mutable and immutable references simultaneously
- Changing mutable ref to immutable without reason
- Making variable immutable but still trying to mutate
Ready to prove it?
Pass the test and this pattern moves unknown
