What if you could fix one number and change your whole program instantly?
Why Constants in Rust? - Purpose & Use Cases
Imagine you are writing a program that uses the value of pi many times. You type 3.14159 everywhere by hand.
Typing the same number repeatedly is slow and easy to make mistakes. If you want to change pi's value, you must find and fix every spot manually.
Constants let you store a fixed value once with a name. You use the name everywhere, so the value stays the same and is easy to update.
let area = 3.14159 * radius * radius; let circumference = 2.0 * 3.14159 * radius;
const PI: f64 = 3.14159; let area = PI * radius * radius; let circumference = 2.0 * PI * radius;
Constants make your code clearer, safer, and easier to change without hunting for repeated values.
In a game, you can define a constant for the maximum player health. If you want to change it later, you update one place, and the whole game uses the new value.
Constants store fixed values with names.
They prevent mistakes from repeated numbers.
Changing a constant updates all uses automatically.
Practice
Solution
Step 1: Understand constant properties in Rust
Constants in Rust must have an explicit type and are immutable by design.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.Final Answer:
Constants must have a type and cannot be changed after declaration. -> Option BQuick Check:
Constants are immutable with explicit types = B [OK]
- Thinking constants can be changed like variables
- Omitting the type annotation for constants
- Assuming constants are only for inside functions
MAX_POINTS with value 100 in Rust?Solution
Step 1: Recall Rust constant syntax
Constants use the keywordconst, require a type annotation, and use an equals sign to assign a value.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 keywordvar,let MAX_POINTS: i32 = 100;usesletfor variables.Final Answer:
const MAX_POINTS: i32 = 100; -> Option DQuick Check:
Constants needconst+ type + value = D [OK]
- Forgetting the type annotation
- Using let or var instead of const
- Missing the semicolon at the end
const PI: f64 = 3.14;
fn main() {
println!("Value of PI is {}", PI);
}Solution
Step 1: Understand constant declaration and usage
The constantPIis declared with typef64and value3.14. It is used insideprintln!macro.Step 2: Predict output of the print statement
The print statement will replace{}with the value ofPI, which is3.14. No errors occur.Final Answer:
Value of PI is 3.14 -> Option AQuick Check:
Printing constant value outputs its assigned number = A [OK]
- 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
const MAX: i32 = 10;
fn main() {
MAX = 20;
println!("{}", MAX);
}Solution
Step 1: Analyze constant assignment
Constants in Rust are immutable and cannot be assigned a new value after declaration.Step 2: Check the code for errors
The lineMAX = 20;tries to change the constant, which causes a compile-time error.Final Answer:
Cannot assign a new value to a constant after declaration. -> Option CQuick Check:
Constants are immutable; reassignment causes error = C [OK]
- Trying to change a constant's value
- Thinking constants behave like variables
- Ignoring compiler error messages about immutability
Solution
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 typeu32and value 7, which is correct syntax.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 } usesletwhich 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 } usesstaticand adds weeks and days, which is incorrect logic.Final Answer:
const DAYS_IN_WEEK: u32 = 7; fn total_days(weeks: u32) -> u32 { weeks * DAYS_IN_WEEK } -> Option AQuick Check:
Correct const declaration and usage in calculation = A [OK]
- Using let instead of const for fixed values
- Omitting type annotation in const declaration
- Using addition instead of multiplication for total days
Ready to prove it?
Pass the test and this pattern moves unknown
