Mutation and References in Rust

Mutation and References in Rust

·

3 min read

Rust is immutable by default, which implies that once a variable is declared, it cannot be modified. Understanding mutation in Rust is crucial since it contributes to a deeper understanding of the language.

We will discuss how mutability and references function in rust in this article. Make use of Rust Playground or your preferred IDE to follow along.

Handling Mutation in Rust

As we already know, variables are heavily used in rust because they are used to hold data. Let's declare a variable and try to change the data in the code block below:

fn main() {
    let a = "Rust";
        a = "Easy Rust";

    println!("{}", a);
}

The compiler complains in the preceding code block because variables cannot be mutated by default.

To mutate a variable, add mut immediately after the let keyword:

fn main() {
    let mut a = "Rust";
        a = "Easy Rust";

    println!("{}", a);
}

To ensure that you get the desired result, you must change the variable's data to the same type of variable that was declared. The output in this case is also a type of &str.

References In Rust

Both references and mutation are crucial in rust. Rust ensures that every memory access is secure by using references. Rust uses & to establish a reference.

First, let's create a scenario in which we assign a declared variable to a different variable and print the variable it was reassigned to.

fn main() {
    let brand = String::from("Toyota");

    let car = brand;

    println!("{}", car);
}

This prints out Toyota to the console.

Now, let's attempt to also print the variable brand to the console as well.

fn main() {
    let brand = String::from("Toyota");

    let car = brand;

    println!("{}", car);
    println!("{}", brand);
}

We instantly get an error that the value variable brand has been moved to car.

The only way brand retains its value is to reference it to car. This way, the value is borrowed from brand to car.

fn main() {
    let brand = String::from("Toyota");

    let car = &brand; //reference added 

    println!("{}", car);
    println!("{}", brand);
}

Mutable References in Rust

Mutating references in rust is possible by adding mut to the declared variable and also adding &mut to the second variable, which represents a mutable reference.

fn main() {
    let mut my_number = 8; // don't forget to write mut here!
    let num_ref = &mut my_number;
    let calc = *num_ref + 10;

    println!("{:?}", calc);
    println!("{}", my_number)
}

In the above code block, we added mut to my_number, and assigned it to num_ref as a mutable reference. num_ref holds a mutable reference of my_number, to make use of num_ref we have to dereference it by adding * in front of the variable to get access to my_number, which is a type of i32 else it will be a type of &mut i32.

Conclusion

The fundamentals needed to comprehend mutations and references in rust were addressed in this article. Visit the following websites to learn more about Mutations and References in Rust: