In Rust, constants are defined using const and must have a fixed type and value known at compile time. Unlike mutable variables, constants never change and can be used throughout your code wherever needed. Use const for values like limits, defaults, or settings that should stay the same.
Once you understand how variables work in Rust, the next important topic is constants. Constants are different from regular variables. They are always immutable and must be declared using the const keyword. In this post, I will explain how constants work, how they differ from variables, and when to use them.
If you have not already read about Variables and Mutability in Rust, please go through that first.
What Is a Constant in Rust?
A constant is a value that cannot change and is defined at compile time. You declare it using const, like this:
const MAX_USERS: u32 = 100;
In this example, MAX_USERS is a constant of type u32 (an unsigned 32-bit integer). It cannot change during the program.
How Is const Different from let?
Here is how const and let are different:
Feature | const | let |
---|---|---|
Value | Fixed at compile time | Can be computed at runtime |
Type | Must be declared | Can be inferred |
Scope | Global or local | Usually local |
Mutability | Always immutable | Mutable if marked with mut |
For example:
const PI: f64 = 3.1415;
let radius = 5;
let area = PI * (radius * radius);
You can use const inside or outside functions. Most often, you define them at the top of your file for values used in multiple places.
Syntax Rules for Constants
When defining a constant:
- You must specify the type
- The value must be known at compile time
- Use ALL_CAPS naming by convention
Valid constant:
const SECONDS_IN_MINUTE: u32 = 60;
Invalid constant (this gives an error because the value is not constant):
let x = 5;
const Y: u32 = x * 2; // This does not compile
When Should You Use const?
Use const when the value:
- Never changes
- Is used in many places
- Should be clearly defined and easy to update
Examples:
- MAX_RETRIES
- DEFAULT_TIMEOUT
- API_VERSION
Example Program
const SITE_NAME: &str = "CodeForGeek";
const MAX_LOGIN_ATTEMPTS: u8 = 3;
fn main() {
println!("Welcome to {}", SITE_NAME);
println!("You have {} attempts to log in", MAX_LOGIN_ATTEMPTS);
}
This example uses two constants. One stores a string, the other stores a number. They can be used just like variables, but they will never change.
const vs static in Rust
There is also a static keyword in Rust, but you rarely need it. It defines a value with a fixed location in memory. Use const for most constant values.
Only use static when you need a value that lasts for the full life of the program and needs to be referenced globally. In almost all beginner and intermediate cases, const is enough.
Summary
Constants in Rust are defined using const, always require a type, and hold values that never change. They are useful for defining shared, fixed values like limits or settings. Use them to write cleaner and more maintainable code.