Concept Flow - Constants
Declare constant with 'const'
Assign fixed value
Use constant in code
Value cannot change
Program runs
Constants are declared once with 'const', assigned a fixed value, and used without changing throughout the program.
const MAX_POINTS: u32 = 100_000; fn main() { println!("Max points: {}", MAX_POINTS); }
| Step | Action | Value/Result | Notes |
|---|---|---|---|
| 1 | Declare constant MAX_POINTS | MAX_POINTS = 100_000 | Constant set once, cannot change |
| 2 | Enter main function | Start main | Program begins execution |
| 3 | Print MAX_POINTS | Output: Max points: 100000 | Uses constant value |
| 4 | End program | Program ends | No changes to MAX_POINTS |
| Variable | Start | After 1 | After 2 | Final |
|---|---|---|---|---|
| MAX_POINTS | undefined | 100000 | 100000 | 100000 |
Constants in Rust: - Declared with 'const' keyword - Must have explicit type - Assigned fixed value at compile time - Immutable: value cannot change - Accessible globally or in module scope