All right, sportsfans. Let’s talk about the variables of Rust!

fn main() {
    let number = 10;
    // ok, pretty straightforward. `number` means the number 10.
    let mut a_mutable_number = 10;
    // `mut` is short for `mutable`; this means it can change.
    // `a_mutable_number` is 10 now, but might not be 10 later.
    let typed_number: i32 = 10;
    // a bit weird. i32 is a number type (32 bit number).
    // `typed_number` is a specific kind of number.
    // They wear a jersey with a signed 32 on it and play for the integer team.
    let reference_number = &mut a_mutable_number;
    // weirder. `mut` swapped locations and married an ampersand. Why?
    // Maybe it's time I start playing around with them.
}

So, let’s start with the boring let number = 10 and do stuff to it.

println!("{}", number); // outputs 10
println!("{}", number + 1); // outputs 11
number += 1;
println!("{}", number);
// from the compiler: "error: re-assignment of immutable variable `number`"

First two examples are straightforward. Incrementing the number is technically redefining it, which prompts the compiler to complain about trying to change something that’s technically immutable. If I can’t change this, can I change a_mutable_number?

a_mutable_number += 1;
println!("{}", a_mutable_number); // 11! SUCCESS!

I can. Sweet. Now what about the typed_number?

println!("{}", typed_number + 1); // 11, cool.
println!("{}", typed_number + 1.0); // Another error. Something about types.

If I give it mutability, can I change the type?

let mut typed_number: i32 = 10;
typed_number = 1.0; // error. Again, about the types! Always with the types!

Looks like I can’t. And as for that last variable binding example from back up top?

let mut number = 10;
let reference_number = &mut number;
// error. cannot borrow `number` as immutable because it is also borrowed as mutable
let another_number = 10;
let reference_number = &mut another_number;
// error: cannot borrow immutable local variable `another_number` as mutable

Haha. What? I guess this is something I’ll have to revisit later on. With all these mentions about types I think that’s what I’ll dive into next.

Conclusion: :sweat_smile: :loop: