Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Rust Dictionary

  1. Home
  2. Rust Dictionary
  3. let / mut / const / static

let / mut / const / static

Since: Rust 1.0(2015)

In Rust, variables are declared with let and are immutable by default. Use mut to make a variable mutable. const and static define constants and static variables that are valid throughout the entire program.

Syntax

let variable_name: Type = value;
let variable_name = value; // Uses type inference.

// Mutable variable
let mut variable_name = value;

// Constant (type annotation required; evaluated at compile time)
const CONSTANT_NAME: Type = value;

// Static variable (has a fixed memory location valid for the entire program)
static VARIABLE_NAME: Type = value;

// Shadowing (re-declares a variable with the same name)
let variable_name = value;
let variable_name = new_value; // Hides the previous variable.

Declaration Types

DeclarationMutabilityDescription
let x = valueImmutableThe default variable declaration. Reassignment is not allowed.
let mut x = valueMutableA variable that can be reassigned and modified.
const NAME: Type = valueImmutableA constant evaluated at compile time. A type annotation is required.
static NAME: Type = valueImmutableA static variable with a fixed memory address. Has a 'static lifetime.
ShadowingImmutable → ImmutableRe-declares a variable with the same name, allowing you to change its value or type. The original variable is hidden.

Sample Code

sample_let_mut_const.rs
const MAX_POINTS: u32 = 100_000;
static GREETING: &str = "Hello";

fn main() {
    // Immutable variable
    let x = 5;
    // x = 6; // Error: cannot assign to immutable variable.

    // Mutable variable
    let mut y = 5;
    y = 6; // Reassignment is allowed.
    println!("y = {}", y); // Prints "6".

    // Type inference
    let z = 3.14; // Inferred as f64.
    println!("z = {}", z);

    // Shadowing (the type can also change)
    let spaces = "   "; // &str type
    let spaces = spaces.len(); // usize type (the type has changed)
    println!("spaces = {}", spaces); // Prints "3".

    // Stepwise transformation with shadowing
    let input = "  42  ";
    let input = input.trim(); // Removes surrounding whitespace.
    let input: i32 = input.parse().unwrap(); // Converts to a number.
    println!("input = {}", input); // Prints "42".

    // Shadowing inside a scope (does not affect the outer variable)
    let value = 10;
    {
        let value = value * 2; // Only valid within this block.
        println!("inner value = {}", value); // Prints "20".
    }
    println!("outer value = {}", value); // Still "10".

    // Locks a mutable variable as immutable via shadowing
    let mut count = 0;
    count += 1;
    let count = count; // Reassignment is no longer allowed.

    println!("MAX: {}", MAX_POINTS);
    println!("{}", GREETING);
    println!("count: {}", count);
}

Compile with the following command:

rustc let_mut_const.rs
./let_mut_const
y = 6
z = 3.14
spaces = 3
input = 42
inner value = 20
outer value = 10
MAX: 100000
Hello
count: 1

Common Mistakes

Common mistake 1: trying to reassign an immutable variable

Variables in Rust are immutable by default. Reassignment requires the mut keyword.

fn main() {
    let x = 5;
    // Compile error: cannot assign twice to immutable variable `x`
    // x = 6;

    let mut y = 5;
    y = 6; // OK: y is mutable
    println!("y = {}", y); // 6
}

Common mistake 2: confusing shadowing with reassignment

Shadowing (let x = new_value) creates a new variable and can change the type. Mutation (mut) modifies the same variable in place and cannot change its type.

fn main() {
    // Shadowing: can change the type (&str → usize)
    let value = "42"; // &str
    let value = value.parse::<usize>().unwrap(); // usize — a new variable
    println!("value: {}", value); // 42

    // Mutation: type stays the same
    let mut count = 0i32;
    count = 10; // OK
    // count = "hello"; // Compile error: mismatched types
    println!("count: {}", count); // 10
}

Notes

Because variables are immutable by default in Rust, unintended modifications are prevented. Using mut only when mutability is truly needed makes the intent of your code explicit.

Shadowing differs from mut in that it is useful for type conversion and reuse of names within a scope. For example, when parsing a string input through stages like trimming and converting to a number, you don't need to create separate variable names for each step. Shadowing inside a block scope also does not affect the outer variable, making it safe for temporary calculations. However, when shadowing a mut variable to remove its mutability, it is common practice to leave an explicit comment to document the intent.

You can use underscores (_) as separators in numeric literals (e.g., 100_000).

If you find any errors or copyright issues, please .