๐ Scenario: Imagine you are writing a program to store the number of days in a week. This number should never change because it is a fixed fact.
๐ฏ Goal: You will create an immutable variable to hold the number of days in a week, then try to use it in a simple calculation.
๐ What You'll Learn
Create an immutable variable named days_in_week with the value 7.
Create another variable named weeks with the value 3.
Calculate the total number of days in weeks weeks using days_in_week.
Print the total number of days.
๐ก Why This Matters
๐ Real World
Immutable variables are used to store fixed values that should not change, like constants or configuration values.
๐ผ Career
Understanding immutability is important for writing safe and predictable Rust programs, which is valuable in systems programming and software development.
Progress0 / 4 steps
1
Create an immutable variable
Create an immutable variable called days_in_week and set it to 7.
Rust
Hint
Use let without mut to make the variable immutable.
2
Add a variable for weeks
Add a variable called weeks and set it to 3 inside the main function, below days_in_week.
Rust
Hint
Use let weeks = 3; to create the variable.
3
Calculate total days
Create a variable called total_days that multiplies days_in_week by weeks inside the main function.
Rust
Hint
Use let total_days = days_in_week * weeks; to calculate the total.
4
Print the total days
Print the value of total_days using println! inside the main function.
Rust
Hint
Use println!("Total days: {}", total_days); to print the result.
Practice
(1/5)
1. What does it mean when a variable is immutable in Rust?
easy
A. It can be changed anytime during the program.
B. Its value cannot be changed after it is set.
C. It is only accessible inside a function.
D. It automatically becomes mutable after declaration.
Solution
Step 1: Understand Rust variable default behavior
In Rust, variables are immutable by default, meaning their values cannot be changed once assigned.
Step 2: Interpret immutability meaning
Immutability means the value is fixed and cannot be reassigned or modified after initialization.
Final Answer:
Its value cannot be changed after it is set. -> Option B
Quick Check:
Immutable means unchangeable = A [OK]
Hint: Immutable means no changes allowed after assignment [OK]
Common Mistakes:
Thinking immutable means variable is hidden or local
Confusing immutability with mutability
Assuming variables change automatically
2. Which of the following is the correct way to declare a mutable variable named count with value 10 in Rust?
easy
A. let mut count = 10;
B. let count = 10;
C. mut let count = 10;
D. count mut = 10;
Solution
Step 1: Recall Rust syntax for mutable variables
In Rust, to make a variable mutable, you write let mut variable_name = value;.
Step 2: Check each option
let mut count = 10; uses the correct syntax. The other options use incorrect syntax.
Final Answer:
let mut count = 10; -> Option A
Quick Check:
Mutable variable syntax = let mut name = value [OK]
Hint: Use 'let mut' before variable name for mutability [OK]
Common Mistakes:
Placing 'mut' after variable name
Using 'mut let' instead of 'let mut'
Omitting 'mut' for mutable variables
3. What will be the output of this Rust code?
fn main() {
let x = 5;
// x = 10; // This line is commented out
println!("{}", x);
}
medium
A. 10
B. Compilation error
C. Runtime error
D. 5
Solution
Step 1: Analyze variable declaration and assignment
Variable x is declared as immutable with value 5. The reassignment line is commented out, so it does not execute.
Step 2: Understand the print statement
The program prints the value of x, which remains 5.
Final Answer:
5 -> Option D
Quick Check:
Immutable variable unchanged prints original value = 5 [OK]
Hint: Commented reassignment means original value prints [OK]
Common Mistakes:
Assuming reassignment happens despite comment
Expecting compilation error due to reassignment line
Confusing output with reassignment value
4. Identify the error in this Rust code snippet:
fn main() {
let x = 3;
x = 4;
println!("{}", x);
}
medium
A. Cannot assign to immutable variable `x`
B. Variable `x` is not declared
C. println! macro syntax is incorrect
D. Missing semicolon after let statement
Solution
Step 1: Check variable mutability
Variable x is declared immutable by default with let x = 3;.
Step 2: Identify reassignment error
Trying to assign x = 4; causes a compile-time error because x is immutable.
Final Answer:
Cannot assign to immutable variable `x` -> Option A
5. You want to create a counter that increases by 1 each time a function is called. Which of these Rust code snippets correctly uses an immutable variable to achieve this behavior?
hard
A. Use let mut count = 0; and increment inside the function.
B. Use let count = 0; and reassign count = count + 1; inside the function.
C. Use let count = 0; and return count + 1 without changing count.
D. Use let mut count = 0; but never increment it.
Solution
Step 1: Understand immutability and counter logic
An immutable variable cannot be changed, so you cannot increment it directly.
Step 2: Analyze options for correct usage
Use let count = 0; and return count + 1 without changing count. uses an immutable variable count and returns count + 1 without changing count, which respects immutability and simulates increment.
Final Answer:
Use let count = 0; and return count + 1 without changing count. -> Option C
Quick Check:
Immutable means no change; return new value instead = A [OK]
Hint: Immutable vars can't change; return new value instead [OK]
Common Mistakes:
Trying to reassign immutable variable
Forgetting to declare variable mutable when needed
Ignoring that immutable variables can't be incremented
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.