Concept Flow - Why variables are needed
Start Program
Declare Variable
Assign Value
Use Variable in Code
Change Value (Optional)
Use Updated Value
End Program
This flow shows how variables store values to use and change during a program.
fn main() {
let mut age = 10;
println!("Age is {}", age);
age = 15;
println!("New age is {}", age);
}| Step | Action | Variable 'age' | Output |
|---|---|---|---|
| 1 | Declare mutable variable 'age' with value 10 | 10 | |
| 2 | Print 'Age is 10' | 10 | Age is 10 |
| 3 | Change 'age' value to 15 | 15 | |
| 4 | Print 'New age is 15' | 15 | New age is 15 |
| 5 | Program ends | 15 |
| Variable | Start | After Step 1 | After Step 3 | Final |
|---|---|---|---|---|
| age | undefined | 10 | 15 | 15 |
Variables store values to use and change in programs. In Rust, variables are immutable by default. Use 'mut' to allow changing a variable's value. Variables make code flexible and readable. You can print and update variables anytime.
score with value 10 in Rust?let keyword to declare variables.let score = 10; which is correct Rust syntax. Others use syntax from other languages.let mut count = 5;
count = count + 3;
println!("{}", count);count is declared with mut, so it can be changed.let x = 10;
x = 20;
println!("{}", x);x is declared without mut, so it is immutable.20 to x causes an error because x cannot be changed.x -> Option A1. let score = 0; 2. let mut score = 0; 3. var score = 0; 4. const score = 0;
mut in Rust.let mut score = 0; which allows updates. Line 1 is immutable, line 3 uses invalid syntax, line 4 is constant and cannot change.Pass the test and this pattern moves unknown