Constants
Variables get their values while the program runs. But some values are known before the program starts and never change: the seconds in an hour, the top level in a game, the size of a game board.
Such a value is like a box sealed at the factory: its contents are decided when the program is built, and it cannot be opened later. For such values Rust has constants:
const MAX_LEVEL: i32 = 100;
const SECONDS_PER_HOUR: i32 = 60 * 60;
fn main() {
let level = 7;
println!("{level} / {MAX_LEVEL}"); // 7 / 100
println!("{SECONDS_PER_HOUR}"); // 3600
}Let's take the first line apart:
constis the keyword: "I declare a constant";MAX_LEVELis the name: constants are written in capital letters, with words separated by underscores;: i32is the type, which a constant always states;= 100is the value.
In the second line, the value is given by the expression 60 * 60. The compiler computes it during the build, and the ready number 3600 goes into the program. Both constants are declared above main, outside any function, so you can use them anywhere in the file.
The compiler watches the style of the name. A constant with lowercase letters, such as max_level, still builds, but the compiler gives a warning.
How const differs from let
A variable from let without mut does not change either. But a constant has stricter rules:
- the type is always written; the compiler does not infer it;
- the value must be known at build time, so a variable cannot be used in it:
const LIMIT: i32 = level * 2;gives the errorerror[E0435]: attempt to use a non-constant value in a constant; - a constant can be declared outside functions, while
letdoes not work there; - a constant never has
mut.
Interviewers often ask how an immutable variable differs from a constant. The short answer: the value of let is computed while the program runs, and the value of const is computed at build time.
Here the sealed-box analogy stops working. A constant is not a box at all: the compiler writes its value into every place where its name appears. After the build, the line with {MAX_LEVEL} works as if the number 100 itself were written there.
const and static
Constants have a relative: static. It also requires a type and a value known at build time, but it creates a real box: one place in memory that exists while the program runs.
const MAX_LEVEL: i32 = 100;
static MAX_PLAYERS: i32 = 4;
fn main() {
println!("{MAX_LEVEL} {MAX_PLAYERS}"); // 100 4
}Interviewers like this question: how does const differ from static? The answer:
constis a value the compiler substitutes into every place where it is used;staticis one variable with a fixed place in memory for the whole time the program runs;constis the default choice, andstaticis for when you need exactly one place in memory.
Common mistake
The constant's type was forgotten:
const MAX_LEVEL = 100; // ✗
fn main() {
println!("{MAX_LEVEL}");
}error: missing type for `const` itemWith let the type is inferred from the value, but with a constant it is not. The help: hint in the full message suggests the type : i32 right away, and the fixed line looks like this: const MAX_LEVEL: i32 = 100;.
Key points
constdeclares a value known at build time: the type is required, and the name is written in capital letters.- The compiler substitutes a constant's value into every place where it appears.
- A constant can be declared outside functions, while
letcannot. staticalso requires a type, but it creates one place in memory for the whole time the program runs.