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.