codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🦀

Rust

5 / 58 topics
1Getting Started with Rust2Installing Rust3Hello World Program4Rust Syntax5Variables and Mutability6Data Types7Operators
Tutorials/Rust/Variables and Mutability
🦀Rust

Variables and Mutability

Updated 2026-04-20
3 min read

Introduction

Variables are fundamental building blocks in any programming language, allowing you to store and manipulate data. In Rust, variables have unique characteristics that contribute to the language's safety and performance. This tutorial will explore the concepts of variables and mutability in Rust, providing detailed explanations, code examples, and best practices.

Variables in Rust

In Rust, a variable is declared using the let keyword followed by the variable name and an optional type annotation. If no type is specified, Rust's type inference system determines the type based on the assigned value.

// Declare a variable with explicit type
let x: i32 = 5;

// Declare a variable with implicit type
let y = 10;

Immutable Variables

By default, variables in Rust are immutable. This means that once a variable is bound to a value, it cannot be changed.

fn main() {
    let x = 5;
    // The following line will cause a compile-time error
    x = 6; // Error: cannot assign twice to immutable variable `x`
}

Immutable variables provide safety by ensuring that once a value is set, it remains constant throughout the program. This can help prevent bugs related to unintended changes in data.

Mutable Variables

To make a variable mutable, you need to add the mut keyword after let.

fn main() {
    let mut x = 5;
    println!("The value of x is: {}", x);
    x = 6; // This is allowed because x is mutable
    println!("The new value of x is: {}", x);
}

Mutable variables allow you to change the value after it has been initially set. However, this flexibility comes with a trade-off in terms of safety and predictability.

Shadowing

Rust allows you to shadow a variable by redeclaring it with the same name. This creates a new variable that temporarily replaces the original one within its scope.

fn main() {
    let x = 5;
    let x = x + 1;

    {
        let x = x * 2;
        println!("The value of x in the inner scope is: {}", x); // Prints 12
    }

    println!("The value of x in the outer scope is: {}", x); // Prints 6
}

Shadowing is useful when you need to change the type or value of a variable without creating a new one. However, it can sometimes lead to confusion, so use it judiciously.

Constants

Constants are similar to immutable variables but have some key differences. They are declared using the const keyword and must be annotated with a type. Constants can only hold constant expressions, not the result of function calls or any other runtime computation.

const MAX_POINTS: u32 = 100_000;

Constants are useful for defining values that should remain unchanged throughout the program's execution, such as configuration settings or mathematical constants.

Best Practices

Use Immutable Variables by Default

Rust encourages the use of immutable variables to promote safety and predictability. Only make a variable mutable if you have a specific reason to change its value after initialization.

Minimize Mutable State

Minimizing mutable state can lead to more predictable and easier-to-debug code. Try to encapsulate mutable state within functions or modules where it is necessary.

Use Shadowing Sparingly

Shadowing can be powerful but can also make the code harder to understand. Use shadowing only when you need to change the type or value of a variable in a clear and intentional way.

Choose Appropriate Types

When declaring variables, choose types that accurately represent the data you are working with. This not only improves readability but also helps the compiler optimize your code.

Conclusion

Variables and mutability are essential concepts in Rust that contribute to its safety and performance. By understanding how to declare, use, and manage variables effectively, you can write more robust and efficient Rust programs. Remember to prioritize immutability, minimize mutable state, and use shadowing judiciously to take full advantage of Rust's strengths.

In the next section, we will explore data types in Rust, including primitive types and compound types, providing a comprehensive understanding of how to work with different kinds of data in your programs.


PreviousRust SyntaxNext Data Types

Recommended Gear

Rust SyntaxData Types