0
0
Rustprogramming~20 mins

Constants in Rust - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Rust Constants Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2: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);
}
AMax points: 100_000
BCompilation error: constants must be mutable
CMax points: 100000
DRuntime error: constant not initialized
Attempts:
2 left
๐Ÿ’ก Hint
Constants in Rust are immutable and can be used anywhere after declaration.
โ“ Predict Output
intermediate
2: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);
}
AValue is: 20
BRuntime error: variable VALUE not found
CCompilation error: cannot shadow constant
DValue is: 10
Attempts:
2 left
๐Ÿ’ก Hint
Local variables can shadow constants with the same name.
โ“ Predict Output
advanced
2: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);
}
ACompilation error: cannot multiply constants
BResult: 15
CResult: 8
DRuntime error: constant not initialized
Attempts:
2 left
๐Ÿ’ก Hint
Constants can be computed at compile time using expressions.
โ“ Predict Output
advanced
2: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);
}
ACompilation error: mismatched types between f32 and u32
BArea: 78
CRuntime error: cannot multiply float and integer
DCompilation error: constant AREA not initialized
Attempts:
2 left
๐Ÿ’ก Hint
Rust requires matching types in constant expressions.
๐Ÿง  Conceptual
expert
2: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?
AConstants allow you to use mutable references safely
BConstants can be changed at runtime for flexibility
CConstants are faster because they are stored on the heap
DConstants guarantee immutability and can be used in any scope without allocation
Attempts:
2 left
๐Ÿ’ก Hint
Think about immutability and compile-time guarantees.