Skip to main content

How to Declare a Constant in Rust with const

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

In Rust, const ISSUER: &str = "https://login.example.org"; declares a constant: a value fixed when the program compiles, which no code can change. Unlike let, a constant always needs its type written out, and it can live outside any function.

Syntax​

Syntax
const <NAME>: <type> = <value>;
PartWhat it does
constDeclares a constant, which is always immutable.
<NAME>The constant's name, in capital letters with underscores between words.
<type>The type of the value, such as &str or u32. It can't be left out.
<value>A value the compiler can work out on its own.

The Rust Book, Constants: "You declare constants using the const keyword instead of the let keyword, and the type of the value must be annotated." It adds: "Constants aren't just immutable by default—they're always immutable."

The Example​

The example declares a string constant and a number constant outside main and prints them. Then a changed version tries to assign a new value to the string constant.

Prerequisites​

  • Rust, with Cargo. This guide uses Rust 1.100.1.

Step 1: Create the Project and Write the Code​

Terminal
cargo new demo
cd demo

cargo new demo creates a project with a src/main.rs. Replace its contents:

src/main.rs, which declares const ISSUER as a str and const MAX_RETRIES as a u32 outside main, and prints ISSUER and MAX_RETRIES times 2 inside it.

src/main.rs
const ISSUER: &str = "https://login.example.org";
const MAX_RETRIES: u32 = 3;

fn main() {
println!("{ISSUER}");
println!("{}", MAX_RETRIES * 2);
}

Step 2: Run It​

Terminal
cargo run -q
Output
https://login.example.org
6

-q keeps Cargo quiet, so only the program's output shows. The command is the same in zsh, bash and PowerShell.

Step 3: Try to Change a Constant​

Replace src/main.rs with a version that assigns a new value to ISSUER:

src/main.rs
const ISSUER: &str = "https://login.example.org";

fn main() {
ISSUER = "https://elsewhere.example.org";
println!("{ISSUER}");
}
Terminal
cargo run -q
Output
error[E0070]: invalid left-hand side of assignment
--> src/main.rs:4:12
|
4 | ISSUER = "https://elsewhere.example.org";
| ------ ^
| |
| cannot assign to this expression

For more information about this error, try `rustc --explain E0070`.
error: could not compile `demo` (bin "demo") due to 1 previous error

A zsh terminal where cargo run first prints the issuer and 6, then, for the changed file, fails with error E0070 invalid left-hand side of assignment, cannot assign to this expression.

What Happened​

  • Both constants worked from inside main, though they were declared outside it.
  • The changed version didn't compile. A constant isn't a place in memory you can assign to, so the compiler pointed at ISSUER and said "cannot assign to this expression".

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out the typeerror: missing type for `const` itemWrite it, as in const MAX_RETRIES: u32 = 3;
Writing const muterror: const globals cannot be mutableUse a let mut variable for a value that changes
Setting a constant from a function that runs at run timeThe value must be known when the program compilesUse a let variable, or a const fn

Troubleshooting​

SymptomLikely causeFix
error[E0070]: invalid left-hand side of assignmentCode assigns to a constantUse a let mut variable instead
warning: constant `limit` should have an upper case nameThe constant's name has small lettersRename it in capital letters, such as LIMIT

FAQ​

What's the difference between const and let?​

let makes a variable that lives inside a function and can be made mutable with mut. const makes a value that can be declared anywhere, including outside functions, needs its type, and can never change.

Why does the Book call the type required?​

Rust works out the type of a let variable from how it's used, but not the type of a constant. The Book says: "Just know that you must always annotate the type."

Conclusion​

const NAME: Type = value; gives a name to a value the compiler knows and nobody can change. Declare constants at the top of a file so every function can use them.

Additional Resources​