keyword

Rust const: define a named constant

Use `const` for a named value known at compile time. It is useful for fixed quantities such as the number of minutes in an hour. A constant needs a type annotation. Expected output: 120 Why: The multiplication uses the constant value 60. Common mistake: Do not use a runtime-dependent calculation as a constant initializer.

Formal Syntax & Structure

rust syntaxrust
const NAME: Type = expression;

Working Example

Example Snippetrust
const MINUTES_PER_HOUR: u32 = 60;
fn main() {    println!("{}", 2 * MINUTES_PER_HOUR);}
Rust Reference