Bird
Raised Fist0
Rustprogramming~10 mins

Constants in Rust - Step-by-Step Execution

Choose your learning style10 modes available
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.
Execution Sample
Rust
const MAX_POINTS: u32 = 100_000;

fn main() {
    println!("Max points: {}", MAX_POINTS);
}
This code declares a constant MAX_POINTS and prints its value.
Execution Table
StepActionValue/ResultNotes
1Declare constant MAX_POINTSMAX_POINTS = 100_000Constant set once, cannot change
2Enter main functionStart mainProgram begins execution
3Print MAX_POINTSOutput: Max points: 100000Uses constant value
4End programProgram endsNo changes to MAX_POINTS
💡 Program ends after printing constant value; constants cannot be changed.
Variable Tracker
VariableStartAfter 1After 2Final
MAX_POINTSundefined100000100000100000
Key Moments - 2 Insights
Why can't we change the value of MAX_POINTS after declaring it?
Because MAX_POINTS is declared with 'const', it is immutable and fixed at compile time, as shown in execution_table step 1 where it is set once and never changes.
Can constants be declared inside functions?
Yes, constants can be declared inside functions, but they are then scoped to that function. They remain immutable and fixed at compile time.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of MAX_POINTS at step 3?
Aundefined
B0
C100000
DCannot be determined
💡 Hint
Refer to execution_table row 3 where MAX_POINTS is printed with value 100000.
At which step does the program print the constant value?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Check execution_table row 3 for the print action.
If we try to assign a new value to MAX_POINTS after step 1, what would happen?
ACompile-time error
BThe program runs normally
CRuntime error
DValue changes successfully
💡 Hint
Constants declared with 'const' cannot be changed; see key_moments about immutability.
Concept Snapshot
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
Full Transcript
This visual execution shows how constants work in Rust. First, a constant MAX_POINTS is declared with the 'const' keyword and assigned the value 100000. This value is fixed and cannot be changed later. When the program runs, it enters the main function and prints the constant's value. The constant remains unchanged throughout the program. Constants have explicit types. Trying to change a constant causes a compile-time error.

Practice

(1/5)
1. What is true about constants in Rust?
easy
A. Constants can be changed anytime during program execution.
B. Constants must have a type and cannot be changed after declaration.
C. Constants do not require a type annotation.
D. Constants are only used inside functions.

Solution

  1. Step 1: Understand constant properties in Rust

    Constants in Rust must have an explicit type and are immutable by design.
  2. Step 2: Evaluate each option

    Constants must have a type and cannot be changed after declaration correctly states constants require a type and cannot change. The other options are incorrect because constants cannot be changed anytime during execution, they must have a type annotation, and they can be declared outside functions.
  3. Final Answer:

    Constants must have a type and cannot be changed after declaration. -> Option B
  4. Quick Check:

    Constants are immutable with explicit types = B [OK]
Hint: Constants always need a type and never change [OK]
Common Mistakes:
  • Thinking constants can be changed like variables
  • Omitting the type annotation for constants
  • Assuming constants are only for inside functions
2. Which of the following is the correct syntax to declare a constant named MAX_POINTS with value 100 in Rust?
easy
A. const MAX_POINTS = 100;
B. var MAX_POINTS: i32 = 100;
C. let MAX_POINTS: i32 = 100;
D. const MAX_POINTS: i32 = 100;

Solution

  1. Step 1: Recall Rust constant syntax

    Constants use the keyword const, require a type annotation, and use an equals sign to assign a value.
  2. Step 2: Check each option

    const MAX_POINTS: i32 = 100; matches the correct syntax: const MAX_POINTS: i32 = 100;. const MAX_POINTS = 100; misses the type, var MAX_POINTS: i32 = 100; uses invalid keyword var, let MAX_POINTS: i32 = 100; uses let for variables.
  3. Final Answer:

    const MAX_POINTS: i32 = 100; -> Option D
  4. Quick Check:

    Constants need const + type + value = D [OK]
Hint: Use const with type and value for constants [OK]
Common Mistakes:
  • Forgetting the type annotation
  • Using let or var instead of const
  • Missing the semicolon at the end
3. What will be the output of this Rust code?
const PI: f64 = 3.14;
fn main() {
    println!("Value of PI is {}", PI);
}
medium
A. Value of PI is 3.14
B. Value of PI is PI
C. Compile error: missing type for PI
D. Runtime error

Solution

  1. Step 1: Understand constant declaration and usage

    The constant PI is declared with type f64 and value 3.14. It is used inside println! macro.
  2. Step 2: Predict output of the print statement

    The print statement will replace {} with the value of PI, which is 3.14. No errors occur.
  3. Final Answer:

    Value of PI is 3.14 -> Option A
  4. Quick Check:

    Printing constant value outputs its assigned number = A [OK]
Hint: Constants print their value directly in output [OK]
Common Mistakes:
  • Expecting the name 'PI' to print instead of its value
  • Thinking missing type causes error here (type is given)
  • Confusing runtime errors with compile-time constants
4. Identify the error in this Rust code:
const MAX: i32 = 10;
fn main() {
    MAX = 20;
    println!("{}", MAX);
}
medium
A. Constants cannot be used inside functions.
B. Missing semicolon after constant declaration.
C. Cannot assign a new value to a constant after declaration.
D. The println! macro is used incorrectly.

Solution

  1. Step 1: Analyze constant assignment

    Constants in Rust are immutable and cannot be assigned a new value after declaration.
  2. Step 2: Check the code for errors

    The line MAX = 20; tries to change the constant, which causes a compile-time error.
  3. Final Answer:

    Cannot assign a new value to a constant after declaration. -> Option C
  4. Quick Check:

    Constants are immutable; reassignment causes error = C [OK]
Hint: Constants cannot be reassigned after declaration [OK]
Common Mistakes:
  • Trying to change a constant's value
  • Thinking constants behave like variables
  • Ignoring compiler error messages about immutability
5. You want to define a constant for the number of days in a week and use it in a function to calculate total days for given weeks. Which code correctly uses constants in Rust?
hard
A. const DAYS_IN_WEEK: u32 = 7; fn total_days(weeks: u32) -> u32 { weeks * DAYS_IN_WEEK }
B. let DAYS_IN_WEEK = 7; fn total_days(weeks: u32) -> u32 { weeks * DAYS_IN_WEEK }
C. const DAYS_IN_WEEK = 7; fn total_days(weeks: u32) -> u32 { weeks * DAYS_IN_WEEK }
D. static DAYS_IN_WEEK: u32 = 7; fn total_days(weeks: u32) -> u32 { weeks + DAYS_IN_WEEK }

Solution

  1. Step 1: Check constant declaration correctness

    const DAYS_IN_WEEK: u32 = 7; fn total_days(weeks: u32) -> u32 { weeks * DAYS_IN_WEEK } declares a constant with type u32 and value 7, which is correct syntax.
  2. Step 2: Verify usage in function

    The function multiplies weeks by the constant, correctly calculating total days. let DAYS_IN_WEEK = 7; fn total_days(weeks: u32) -> u32 { weeks * DAYS_IN_WEEK } uses let which is not a constant. const DAYS_IN_WEEK = 7; fn total_days(weeks: u32) -> u32 { weeks * DAYS_IN_WEEK } misses the type annotation. static DAYS_IN_WEEK: u32 = 7; fn total_days(weeks: u32) -> u32 { weeks + DAYS_IN_WEEK } uses static and adds weeks and days, which is incorrect logic.
  3. Final Answer:

    const DAYS_IN_WEEK: u32 = 7; fn total_days(weeks: u32) -> u32 { weeks * DAYS_IN_WEEK } -> Option A
  4. Quick Check:

    Correct const declaration and usage in calculation = A [OK]
Hint: Use const with type and multiply correctly in function [OK]
Common Mistakes:
  • Using let instead of const for fixed values
  • Omitting type annotation in const declaration
  • Using addition instead of multiplication for total days
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard