Challenge - 5 Problems
Rust Constants Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
Output of constant usage in Rust
What is the output of this Rust program that uses a constant?
Rust
const MAX_POINTS: u32 = 100_000; fn main() { println!("Max points: {}", MAX_POINTS); }
Attempts:
2 left
๐ก Hint
Constants in Rust are immutable and can be used anywhere after declaration.
โ Incorrect
The constant MAX_POINTS is set to 100_000, which is just a numeric literal with an underscore for readability. The program prints the value as 100000 without underscores.
โ Predict Output
intermediate2:00remaining
Constant shadowing and output
What will this Rust code print when run?
Rust
const VALUE: i32 = 10; fn main() { let VALUE = 20; println!("Value is: {}", VALUE); }
Attempts:
2 left
๐ก Hint
Local variables can shadow constants with the same name.
โ Incorrect
The local variable VALUE shadows the constant VALUE inside main, so the printed value is 20.
โ Predict Output
advanced2:00remaining
Constant expression evaluation
What is the output of this Rust program using constant expressions?
Rust
const BASE: u32 = 5; const MULTIPLIER: u32 = 3; const RESULT: u32 = BASE * MULTIPLIER; fn main() { println!("Result: {}", RESULT); }
Attempts:
2 left
๐ก Hint
Constants can be computed at compile time using expressions.
โ Incorrect
The constant RESULT is computed as 5 * 3 = 15 at compile time and printed.
โ Predict Output
advanced2:00remaining
Constant type mismatch error
What error does this Rust code produce?
Rust
const PI: f32 = 3.14; const RADIUS: u32 = 5; const AREA: u32 = PI * RADIUS * RADIUS; fn main() { println!("Area: {}", AREA); }
Attempts:
2 left
๐ก Hint
Rust requires matching types in constant expressions.
โ Incorrect
The constant AREA tries to multiply a float (PI) with integers (RADIUS), causing a type mismatch error at compile time.
๐ง Conceptual
expert2:00remaining
Why use constants instead of variables in Rust?
Which of the following is the best reason to use a constant instead of a variable in Rust?
Attempts:
2 left
๐ก Hint
Think about immutability and compile-time guarantees.
โ Incorrect
Constants in Rust are immutable, have no fixed memory address, and can be used anywhere without runtime overhead, unlike variables.