Page Content

Tutorials

Rust variables and Mutable variables

Rust Variables

Rust names its variables “memory storage.” They provide symbolic names, which are easier to remember than memory addresses. Variables are declared using the let statement. A declaration using let creates a binding between the variable name and a memory location.

Rust Installation Procedure on Windows

Syntaxes for declaring a variable with let:

let varname:type=value;let var1: i8 = 10;  full declaration
let varname=value;let var2 = 11; type inference
let varname:type;let var3: i8; uninitialized variable
let varname;let var4; type inference and uninitialized
var3 = 12;  delayed initialization
var4 = 13;  delayed initialization

Variables initialized before they are used. Initialization can happen within the let statement or later via assignment. The type of the variable is set either clearly within the let statement or through type inference, and either way, the variable is statically typed. Creating a variable and assigning a value with let is considered a statement.

fn main() {
    let x = 5;
    println!("The value of x is: {x}");
}

After run the above program the output given “The value of x is:5”

fn main() {
    let x = 5;
    let y=3;
    println!("The value of x,y is: {},{}",x,y);
}

After run the program the putput is The value of x,y is: 5,3

If you change from println!(“The value of x,y is: {},{}”,x,y); to println!(“The value of x,y is: {},{}”,y,x);  the output is The value of x,y is: 3,5

fn main() {
    x = 6;
    println!("The value of x is: {}",x);
}

After run the above program the output given “error[E0425]: cannot find value `x` in this scope”

Rust mutable variable

By default, variables in Rust are immutable. This means that once a value is bound to a variable name, you cannot change that value. The compiler guarantees that a value declared as immutable will not change. This makes code easier to reason through, as you don’t have to track how or where a value might change.

Mutability can be very useful, however, it sometimes makes code more convenient to write. To make a variable mutable, you add the mut keyword in front of the variable name. The mut keyword is an abbreviation for “mutable.”.

Why are Rust variables immutable by default?

Here are the main reasons Rust variables are immutable by default:

  •  Safety and Security: Immutability by default enhances safety and security in Rust. It protects set variables against inadvertent modifications. The Rust compiler guarantees that a declared value won’t change, unlike other languages. A compile-time error while changing an immutable variable helps identify errors early, especially when one section of the code expects a constant value while another part changes it.
  • Easier to Reason About Code: Why Immutable variables simplify code reasoning by eliminating the need to follow value changes from reading or writing code. The code is easier to comprehend and reason through.
  •  Preventing Data Races: Rust’s immutability default enables fearless concurrency and prevents data races during build time. Data races occur when several pointers access the same data concurrently, at least one writing, and no synchronisation. Mutable static variables are dangerous in Rust because globally accessible mutable data is hard to handle without data races. Immutable data is safe to exchange between threads.
  •  Conveying Intent: Using the mut keyword to make a variable changeable directly informs readers of the planned change. Readers benefit from this tip.
  • Performance Benefits: While safety and concurrency are important motivations, forced immutability may also provide performance benefits.

Mutability can be used with allow to switch from immutability, which is the default and typically chosen for its benefits.

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

When run the program using cargo run, receive an error message about an immutability error.

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

When run, this code would print:

The value of x is: 5

The value of x is: 6

The above example shows the value bound to x was successfully changed from 5 to 6 because mut was used. If x was not declared with mut, the assignment x = 6; would result in a compile error. Adding mut also conveys intent to future readers of the code that the variable’s value might change.

Check here first Rust program

How to write Cargo program in Rust

Index