๐ Scenario: Imagine you are keeping track of the number of apples in a basket. Sometimes apples are added or taken away. You want to write a program that can change the number of apples as needed.
๐ฏ Goal: You will create a mutable variable to hold the number of apples, change its value, and then print the final count.
๐ What You'll Learn
Create a mutable variable called apples with the initial value 5.
Change the value of apples to 10.
Print the value of apples.
๐ก Why This Matters
๐ Real World
Mutable variables let programs keep track of changing information, like counting items or updating scores.
๐ผ Career
Understanding mutable variables is essential for writing programs that handle data that changes over time, a common task in software development.
Progress0 / 4 steps
1
Create a mutable variable
Create a mutable variable called apples and set it to 5.
Rust
Hint
Use let mut to create a variable that can change.
2
Change the variable value
Change the value of the mutable variable apples to 10.
Rust
Hint
Assign a new value to apples without using let.
3
Print the variable
Print the value of the variable apples using println!.
Rust
Hint
Use println!("{}", apples); to show the value.
4
Run and see the output
Run the program to see the final number of apples printed.
Rust
Hint
The program should print 10 because that is the final value of apples.
Practice
(1/5)
1. What keyword do you use in Rust to make a variable mutable?
easy
A. let
B. var
C. change
D. mut
Solution
Step 1: Understand variable declaration in Rust
Rust uses let to declare variables, but by default they are immutable.
Step 2: Identify how to make variables mutable
Adding mut after let makes the variable mutable.
Final Answer:
mut -> Option D
Quick Check:
Use let mut for mutable variables [OK]
Hint: Remember: let mut makes variables changeable [OK]
Common Mistakes:
Using 'var' instead of 'mut'
Forgetting 'mut' and expecting change
Using 'let' alone for mutable variables
2. Which of the following is the correct syntax to declare a mutable integer variable named x with value 5 in Rust?
easy
A. let mut x = 5;
B. var mut x = 5;
C. mut let x = 5;
D. let x = 5;
Solution
Step 1: Recall correct order of keywords
In Rust, let comes first, then mut to mark mutability.
Step 2: Check each option's syntax
let mut x = 5; uses let mut x = 5; which is correct. Others have wrong order or invalid keywords.
Step 1: Understand variable initialization and mutation
The variable count starts at 1 and is mutable.
Step 2: Calculate the new value after addition
Adding 2 to 1 gives 3, so count becomes 3.
Final Answer:
3 -> Option A
Quick Check:
Mutable variable updated correctly outputs 3 [OK]
Hint: Mutable lets you add and update variable values [OK]
Common Mistakes:
Expecting original value without mutation
Thinking code causes error without 'mut'
Confusing output with variable name
4. Identify the error in this Rust code snippet:
fn main() {
let x = 10;
x = 20;
println!("{}", x);
}
medium
A. Missing semicolon after println!
B. Variable x is not declared
C. Cannot assign twice to immutable variable x
D. Incorrect println! syntax
Solution
Step 1: Check variable mutability
Variable x is declared without mut, so it is immutable.
Step 2: Analyze reassignment attempt
Assigning x = 20; tries to change an immutable variable, causing a compile error.
Final Answer:
Cannot assign twice to immutable variable x -> Option C
Quick Check:
Immutable variables cannot be reassigned [OK]
Hint: Add mut to allow variable reassignment [OK]
Common Mistakes:
Ignoring need for 'mut' to reassign
Thinking println! needs semicolon error
Assuming variable is undeclared
5. You want to create a counter that increases by 1 every time a function is called. Which Rust code correctly uses a mutable variable to achieve this?