Recall & Review
beginner
What does it mean when a variable is mutable in Rust?
A mutable variable in Rust means you can change its value after it is first set. You declare it with
mut before the variable name.Click to reveal answer
beginner
How do you declare a mutable variable named
count with the value 5 in Rust?You write: <code>let mut count = 5;</code> This means <code>count</code> can be changed later.Click to reveal answer
beginner
What happens if you try to change a variable that is not mutable in Rust?
Rust will give a compile-time error because variables are immutable by default. You must declare them as mutable to change their value.
Click to reveal answer
intermediate
Why does Rust make variables immutable by default?
Immutability helps prevent bugs by avoiding unexpected changes. It makes programs safer and easier to understand.
Click to reveal answer
beginner
Show an example of changing a mutable variable's value in Rust.
Example:<br><code>let mut score = 10;<br>score = 20;<br>println!("Score is {}", score);</code><br>This prints: <code>Score is 20</code>Click to reveal answer
How do you declare a mutable variable in Rust?
✗ Incorrect
In Rust, you use
let mut to declare a mutable variable.What is the default mutability of variables in Rust?
✗ Incorrect
Variables are immutable by default in Rust unless declared with
mut.What error occurs if you try to change an immutable variable?
✗ Incorrect
Rust prevents changing immutable variables at compile time, so you get a compile-time error.
Why is immutability preferred by default in Rust?
✗ Incorrect
Immutability helps keep code safe and easier to understand by avoiding unexpected changes.
Which of the following is a correct way to change a mutable variable's value?
✗ Incorrect
You must declare the variable as mutable with
mut and then you can assign a new value.Explain what mutable variables are in Rust and how to declare them.
Think about how you can change a variable's value after setting it.
You got /3 concepts.
Describe why Rust makes variables immutable by default and the benefits of this choice.
Consider how fixing values helps avoid mistakes.
You got /4 concepts.