DEV Community

Daniel
Daniel

Posted on

Variables and Mutability

A variable is an alias to the memory address in which some data is stored. In Rust variables are immutable, this is one of many nudges Rust gives you to write your code in a way that takes advantage of the safety and easy concurrency that Rust offers. However, you still have the option to make your variables mutable.
When a variable is immutable, once a value is bound to a name, you can’t change that value.
To illustrate this, generate a new project called variables in your projects directory by using cargo new variables. Then, in your new variables directory, open src/main.rs and replace the code with the following:

fn main() {
    let x = 5;
    println!("The value of x is: {x}");
    x = 6;
    println!("The value of x is: {x}");
}
Enter fullscreen mode Exit fullscreen mode

Save and run the program using cargo run. You should receive an error message regarding an immutability error, as shown in this output:

   Compiling varibales v0.1.0 (/Rust/chapter3/variables)
error[E0384]: cannot assign twice to immutable variable `x`
 --> src/main.rs:4:5
  |
2 |     let x = 5;
  |         -
  |         |
  |         first assignment to `x`
  |         help: consider making this binding mutable: `mut x`
3 |     println!("The value of x is: {x}");
4 |     x = 6;
  |     ^^^^^ cannot assign twice to immutable variable

For more information about this error, try `rustc --explain E0384`.
error: could not compile `varibales` (bin "varibales") due to 1 previous error
Enter fullscreen mode Exit fullscreen mode

You received the error message because you tried to assign a second value to the immutable x variable.
We can solve this by adding mut in front of the variable name. Adding mut also conveys intent to future readers of the code by indicating that other parts of the code will be changing this variable’s value.
Let’s change src/main.rs to the following:

fn main() {
    let mut x = 5;
    println!("The value of x is: {x}");
    x = 6;
    println!("The value of x is: {x}");
}
Enter fullscreen mode Exit fullscreen mode

When we run the program now, we get this:

$ cargo run
   Compiling varibales v0.1.0 (/Rust/chapter3/variables)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 2.86s
     Running `target/debug/varibales`
The value of x is: 5
The value of x is: 6
Enter fullscreen mode Exit fullscreen mode

Shadowing

This is when a new variable is declared with the same name as an already declared variable. The second variable overshadows the first, taking any uses of the variable name to itself until either it itself is shadowed or the scope ends.
Here is an example:

fn main() {
    let  x: u32 = 1;

    {
      let  x = x *2;
      println!("The value of x is: {x}")

    }

    println!("The value of x is: {x}");
}
Enter fullscreen mode Exit fullscreen mode

This program first binds x to a value of 1. Within an inner scope created with the curly brackets, the second let statement shadows x and creates a new variable, multiplying the previous value by 2 to give x a value of 2. When that scope is over, the inner shadowing ends and x returns to being 1. When we run this program, it will output the following:

$ cargo run
   Compiling varibales v0.1.0 (/Rust/chapter3/variables)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 1.95s
     Running `target/debug/varibales`
The value of x is: 2
The value of x is: 1
Enter fullscreen mode Exit fullscreen mode

Constants

A constant is a special type of variable whose value never changes. It stays constant. Constants are always immutable.
A constant is declared using the following syntax in Rust:

const CONSTANT_NAME: data_type = value;
Enter fullscreen mode Exit fullscreen mode

A constant name should be in all uppercase characters and words separated by an underscore. Annotating the data type of the constant is necessary.

Top comments (0)