Skip to content
Theory · Variables and Types · lesson 5 of 12

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:

Rust
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:

  • const is the keyword: "I declare a constant";
  • MAX_LEVEL is the name: constants are written in capital letters, with words separated by underscores;
  • : i32 is the type, which a constant always states;
  • = 100 is 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 error error[E0435]: attempt to use a non-constant value in a constant;
  • a constant can be declared outside functions, while let does 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.

Rust
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:

  • const is a value the compiler substitutes into every place where it is used;
  • static is one variable with a fixed place in memory for the whole time the program runs;
  • const is the default choice, and static is for when you need exactly one place in memory.

Common mistake

The constant's type was forgotten:

Rust
const MAX_LEVEL = 100; // ✗

fn main() {
    println!("{MAX_LEVEL}");
}
TEXT
error: missing type for `const` item

With 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

  • const declares 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 let cannot.
  • static also requires a type, but it creates one place in memory for the whole time the program runs.