The const
keyword is similar to the let
keyword: it allows the creation of an immutable variable. Constants can never be mutable, and they must always be set to a constant expression, i.e., something that has a definite value and that is not the result of a runtime calculation.
const GRAVITY: f32 = 9.801;
const PI: f32 = 22 / 7;
const mut NOT_ALLOWED: u32 = 5; // err: the 'mut' keyword is not allowed with 'const'
Note: the f32
and u32
are type declarations for a 32-bit floating point and 32-bit unsigned integer, respectively. Type definitions are discussed in a separate post.