A constant in Rust is a value that is bound to a name and cannot be changed. It must be set to a constant expression and is always immutable.
Click to reveal answer
beginner
How do you declare a constant in Rust?
You declare a constant using the <code>const</code> keyword, followed by the name in uppercase, a type annotation, and a value. For example: <code>const MAX_POINTS: u32 = 100_000;</code>
Click to reveal answer
beginner
Can constants in Rust be mutable?
No, constants in Rust are always immutable. You cannot change their value after declaration.
Click to reveal answer
intermediate
Where are constants stored in Rust?
Constants are stored directly in the program's binary and embedded at compile time. They do not occupy memory at runtime like variables.
Click to reveal answer
intermediate
What is the difference between a constant and a static variable in Rust?
Constants are always immutable and must be set to a compile-time value. Static variables have a fixed memory location and can be mutable with unsafe code. Constants do not have a fixed memory address.
Click to reveal answer
How do you declare a constant named PI with a value of 3.14 in Rust?
Aconst PI: f64 = 3.14;
Blet PI = 3.14;
Cstatic PI = 3.14;
Dvar PI: f64 = 3.14;
✗ Incorrect
Constants use the const keyword with a type annotation and value.
Can you change the value of a constant after it is declared in Rust?
ANo, constants are immutable.
BOnly inside functions.
CYes, anytime.
DOnly if declared mutable.
✗ Incorrect
Constants are always immutable and cannot be changed.
What is the naming convention for constants in Rust?
Asnake_case
BUPPERCASE_WITH_UNDERSCORES
CcamelCase
DPascalCase
✗ Incorrect
Constants are conventionally named in uppercase letters with underscores.
Which of these is true about constants in Rust?
AThey can be assigned values at runtime.
BThey are stored on the heap.
CThey can be mutable.
DThey must have a type annotation.
✗ Incorrect
Constants must have explicit type annotations and are set at compile time.
What keyword is used to declare a constant in Rust?
Alet
Bvar
Cconst
Dstatic
✗ Incorrect
The const keyword declares constants.
Explain how to declare and use constants in Rust, including naming conventions and immutability.
Think about how constants differ from variables.
You got /5 concepts.
Describe the difference between constants and static variables in Rust.
Consider mutability and memory storage.
You got /4 concepts.
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
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 B
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
Step 1: Recall Rust constant syntax
Constants use the keyword const, 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 keyword var, let MAX_POINTS: i32 = 100; uses let for variables.
Final Answer:
const MAX_POINTS: i32 = 100; -> Option D
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
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.
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.
Final Answer:
Value of PI is 3.14 -> Option A
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
C. Cannot assign a new value to a constant after declaration.
D. The println! macro is used incorrectly.
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 line MAX = 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 C
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?
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.
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.