Rust VariableMut

Rust VariableMut

Variables are the building block of any program. Still, they work differently in different programming languages, the way a variable behaves in Rust is quite different from javascript and other programming languages. Think of variables as wires or connectors; note, Variables are not values.

//a JavaScript variable
let name = "Ayo";

In the above example, we could see that a variable was declared using the "let" keyword in javascript and points to a value called "Ayo."

White and Blue Geometric Photo Finance Facebook Cover.gif

In this image, the variable points to the value; to get to the value, you have to pass through the variable.

One of the specialties of a variable is that it can point to another value; just like a person can change their name, a variable can change its value. This can be called re-assigning; by re-assigning, we pass a new value to the variable "name." Re-assigning works differently in programming languages, and we will look into how Rust does this later on.


let name = "Ayo";
//re-assign a new value "Jeff" to my variable "name"
name = "Jeff";

White and Blue Geometric Photo Finance Facebook Cover (2).gif

So what we did here was to create a new value and assign it to our variable. Notice that you do not have to use the "let" keyword again when re-assigning, and also, we did only change our value, not our variable.

Rust VariableMut

Rust variables are quite different from other programming languages; naturally, rust variables are immutable. Immutability here simply means once a value is pointed to a variable, you can't change that value.

fn main() {
    let name  =  "Ayo";
    name = "Jeff";
}

This code will throw an error in rust language because you cannot re-assign a value to a variable; Rust does this to give us the advantage of safety and easy concurrency. Although mutability can be very useful when building a program, sometimes you might need to re-assign values to variables or even re-assign values to complex data structures; hence, Rust gave us an alternative to having a mutable variable by using the "mut" keyword.

fn main() {
    let mut name = "Ayo";
    name = "Jeff";
}

This code works because we already made it known that we want our variable "name" to be mutable, or we can say re-assignable.

We must note that immutable variables are different from constants, but the similarity here is both constants and variables are immutable, but while you can use the "mut" keyword to make variables in rust mutable, you can't do that for constants in Rust; also, constants name must be in CAPITAL_LETTERS.

fn main(){
    //this is an immutable variable.
    let name = "Ayo";
    //Here is a mutable variable.
    let mut age = 20;
     //here is a constant which can never be mutated.
    const GENDER = "Male";
}

Shadowing Shadowing is feature rust blessed us with and can be used in place of "mut" keyword. Rust allows us to declare a new variable with the same name as a previous variable, this might be new to programmers using other languages

fn main() {

  let x = 10;
 //at this point x is == 10.
  let x = 6;
  //after shadowing x is no longer == 10, x is now == 6.

}

When we shadow, the compiler will only acknowledge that last variable as the valid variable, if we run the code above rust compiler will identify variable x as the owner of value 6. One more thing to note is that assigning a variable to another variable doesn't mean we are mutilating the value the code below shows us what happens when we assign a variable with a value to another variable.

fn main() {
  let x = 5;
  ley y = x;
  println!("This is X {}", x);
 println("This is Y {}", y)
}

What is happening behind the hood here is what rust calls copying, hence we do not mutate the value 5 we only copied the value 5 and assign it to a new variable. Imagine a document you have, but you want the document to be photocopied, once you used the photocopy machine, the information on the original document is going to be the same as the photocopied one and you can't even tell the difference.

Conclusion In this article we understood what variable is and how they work, we were able to explain how Rust handles variables, immutability in Rust, and the "mut" keyword. Finally, we saw some features like copying and shadowing in the Rust language.