How to Declare a Constant in Rust with const
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
const <NAME>: <type> = <value>;
| Part | What it does |
|---|---|
const | Declares 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
cargo new demo
cd demo
cargo new demo creates a project with a src/main.rs. Replace its contents:

const ISSUER: &str = "https://login.example.org";
const MAX_RETRIES: u32 = 3;
fn main() {
println!("{ISSUER}");
println!("{}", MAX_RETRIES * 2);
}
Step 2: Run It
cargo run -q
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:
const ISSUER: &str = "https://login.example.org";
fn main() {
ISSUER = "https://elsewhere.example.org";
println!("{ISSUER}");
}
cargo run -q
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

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
ISSUERand said "cannot assign to this expression".
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Leaving out the type | error: missing type for `const` item | Write it, as in const MAX_RETRIES: u32 = 3; |
Writing const mut | error: const globals cannot be mutable | Use a let mut variable for a value that changes |
| Setting a constant from a function that runs at run time | The value must be known when the program compiles | Use a let variable, or a const fn |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
error[E0070]: invalid left-hand side of assignment | Code assigns to a constant | Use a let mut variable instead |
warning: constant `limit` should have an upper case name | The constant's name has small letters | Rename 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
- Constants, in The Rust Programming Language.
- Constant items, in The Rust Reference.
