01 Rustlings Variables Solution

In Rust, variables are immutable by default. When a variable is immutable, once a value is bound to a name, you can’t change that value. You can however, make a variable mutable by adding mut in front of the variable name.

Further reading:

variables1.rs

// variables1.rs
// Make me compile!
// Execute `rustlings hint variables1` or use the `hint` watch subcommand for a hint.

fn main() {
    let x = 5; // very simple excerize that is missing the `let`
    println!("x has the value {}", x);
}

Adding the let to the statement allows x to bind to 5, very straight forward solution

variables2.rs

// variables2.rs
// Execute `rustlings hint variables2` or use the `hint` watch subcommand for a hint.

fn main() {
    let x: u8 = 5; // you could also just write `let x = 5`
    if x == 10 {
        println!("x is ten!");
    } else {
        println!("x is not ten!");
    }
}

This is basically the opposite of what we saw before with the variable binding in the variables1.rs example, except in this case we're missing the number that x should be bound to.

variables3.rs

// variables3.rs
// Execute `rustlings hint variables3` or use the `hint` watch subcommand for a hint.

fn main() {
    let x: i32 = -123; // adding `= -123` for the code to compile.
    println!("Number {}", x);
}

Since we have x as an i32 let's have some fun and add a negative number. Again this was missing the second half of the statement.

variables4.rs

// variables4.rs
// Execute `rustlings hint variables4` or use the `hint` watch subcommand for a hint.

fn main() {
    let mut x = 3; // mut is the key here
    println!("Number {}", x);
    x = 5; // don't change this line
    println!("Number {}", x);
}

As the opening text declares variables are immutable by default so this code doesn't compile because x is being bound twice to different numbers without declaring it mut, this code will not compile.

variables5.rs

// variables5.rs
// Execute `rustlings hint variables5` or use the `hint` watch subcommand for a hint.

fn main() {
    let number = "T-H-R-E-E"; // don't change this line
    println!("Spell a Number : {}", number);
    let number = 3; // don't rename this variable
    println!("Number plus two is : {}", number + 2);
}

In this exercise we don't rename the variable number but we shadow it by declaring it again with let this allows us to change the type from a &Str to a integer.

variables6.rs

// variables6.rs
// Execute `rustlings hint variables6` or use the `hint` watch subcommand for a hint.


const NUMBER: i32 = 3; // added `:i32`
fn main() {
    println!("Number {}", NUMBER);
}

Constants are another type of variable that is available, but these are always immutable they're declared with const instead of let and types must always be annotated. So we have to add : i32 to make the code compile.

Conclusion

Understanding variables and their mutability is an essential aspect of learning Rust. This blog post covered the Rustlings exercises that demonstrated the usage of variables in different scenarios, including how to declare and shadow variables, work with constants, and use the mut keyword to make variables mutable. These fundamental concepts lay the groundwork for grasping more advanced topics in Rust programming.

As you continue your Rust journey, always remember that variables in Rust are immutable by default, providing safety and predictability in your code. When needed, you can use the mut keyword to allow a variable to be mutable. Constants are also available, providing values that cannot be changed and must have their types annotated.

I encourage you to explore the Rust documentation and practice further exercises to strengthen your understanding of these concepts. Happy coding!