beginnerBasics
Constants and Static
Difference between const and static variables in Rust.
Constants and Static
Difference between const and static variables in Rust.
Difficulty
Beginner
Code
rust
const MAX_POINTS: u32 = 100_000;
static LANGUAGE: &str = "Rust";
fn main() {
println!("Max points: {}", MAX_POINTS);
println!("Language: {}", LANGUAGE);
}Explanation
const is compile-time evaluated. static has fixed memory address and lives for the program duration.
Key Concepts
- Read the code carefully and understand the data flow
- Try modifying the example to see how it changes behavior
- Run this code in the Rust Playground
Related Topics
Browse more examples in the basics category to build a complete understanding of this topic.