Constants
There's another type of variable besides mutable and immutable. They're called "constants".
Constants are like immutable variables with a few extra criteria. We've covered enough to get the idea for the first three:
- They are defined using the
const
keyword instead oflet
- The
mut
keyword can't be used when creating them - Names should always be in UPPER_SNAKE_CASE by convention.
These next three criteria require some knowledge we haven't gotten to yet, We'll touch on them in the next few pages.
- Constants can be declared in global scope (which will see on the next page)
- They must have a type annotation (e.g. the
i32
in the example below which is coming up). - They must be set to something determined at compile time (which we'll also get to shortly)
fn main() {
const ALFA: i32 = 100;
println!("Alfa {ALFA}");
}
CODE RUNNER
TODO
- Examine moving constants to a points after you've talked about scope and types. Probably that makes the most sense.