Rust from the beginning, variables
Tags: rust, beginners, tutorial
Published: 2022-06-07 16:06:43

---
title: Rust from the beginning, variables
published: true
description: In this second part, you'll learn how to work with variables, declare, assign them values and so on.
tags: rust, beginners, tutorial
series : Rust series
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f89plsksh82j3cwhxjtw.jpg
---

> TLDR; this is the second part on Rust. In this part we'll cover variables, how to work with them, change values and so on.

{% embed https://www.youtube.com/watch?v=RvHfraKYyCE %}


## Resources

- [What is Rust, module](https://docs.microsoft.com/en-us/learn/modules/rust-introduction?wt.mc_id=academic-0000-chnoring)
- [Create your first Rust program](https://docs.microsoft.com/en-us/learn/modules/rust-create-program?wt.mc_id=academic-0000-chnoring)
- [Rust Docs](https://www.rust-lang.org/learn)

## Why variables

We use variables to store a value that we want to use later. Additionally, having a variable, a named reference, we're able to better understand what's going on. Here's an example:

```rust
let account_balance = 4000;

println!("Your account balance {}", account_balance);
```

Imagine you didn't have `account_balance` but referred to 4000 in your code, you wouldn't know what 4000 is about.



## Declare variables

To declare a variable in Rust, we need to give it a name and a value, like so:

```rust
let name = "Chris";
```

Note how we start with the keyword `let`, a name for the variable `name`, and a value, "Chris". 

### Type is inferred by value

Values in Rust have a type, and this type is decided at first assignment of a value to the variable. In the below case, `name` is of type string and `age` of type `i32` an integer type:

```rust
let name = "Chris";
let age = 20;
```

When a variable gets a type like this, we call it as the type being _inferred_, meaning that Rust observes what value it's being assigned and makes conclusions based on the assignment.

The type it's inferred is a _default_ type. So, numbers, lacking fraction becomes `i32`, 32-bit integer (whole numbers lacking decimals) for example. If you want to check what default data types, here's some nice code you can use:

```rust
fn type_of<T>(_: &T) {
    println!("{}", std::any::type_name::<T>());
}

fn main() {
  let no = 3;
  let name = "Chris";
  type_of(&no);
  type_of(&name);
}
```

The above code will print `i32` and `string` when run.

### Variables are immutable

Variable values can't be changed by default. You might start assigning a value to a variable like so:

```rust
let account_balance = 4000;
```

and then later you want to change it to say 4500 and attempt to write the following code:

```rust
account_balance = 4500;
```

At this point, you will get an error back, that you can't do this, because it's immutable. You will see just how awesome the Rust compiler is by providing you not only with the error message, but what to do about it as well:

```output
 let account_balance = 4000;
  |       ---------------
  |       |
  |       first assignment to `account_balance`
  |       help: make this binding mutable: `mut account_balance`
7 |   account_balance = 4500;
  |   ^^^^^^^^^^^^^^^^^^^^^^ cannot assign twice to immutable variable
```

The error message suggest we make this _mutable_. Let's explain what that means in the next section.

## Change variable value

Variables are immutable but there are ways to change them. There are two major approaches to change their values:

- **make them mutable**, this involves using the keyword `mut` when declaring the variable. By using `mut`, the compiler is now ok with you changing the variable's value, like so:

   ```rust
   let mut account_balance = 4000;
   account_balance = 4500;
   ```

- **shadowing**, what we do here is declare a variable later down in the code with the exact same name. What happens is that the old variable is "shadowed over" by the name variable and can no longer be referenced to:

   ```rust
   let account_balance = 4000;
   // other code
   let account_balance = 4500; // this version of account_balance shadows the first declared version.
   ```

## Constants

So far, we've been using the `let` keyword to declare variables. Another way to declare variables is by using the `const` keyword. `const` means something different than `let` though. With `const` you declare a variable that should never change. What you are saying in the code is that here's a variable that we don't expect to ever change, like the value of PI or C for the speed of light.

To declare such a variable, we can type like so:

```rust
const PI:f32 = 3.14
```

Notice the usage of `const` and that we explicitly need to set a type, `:f32`, a 32-bit floating-point type. In this case, we choose `f32`, but the default would be `f64` per below compiler recommendation. 

Where you to skip the type, you would get an error like so:

```output
  |
6 |   const PI = 3.14;
  |         ^^ help: provide a type for the item: `PI: f64`
```

There's also another type of constant, `static`, but it's outside the scope of this article, if you want to learn more about that one, [check the docs](https://doc.rust-lang.org/rust-by-example/custom_types/constants.html).

## Summary

In summary, we've learned about bit more on why variables, how to declare them and assign different types of values to them. Additionally, we've seen how variables are immutable but can be changed either by using `mut` keyword or shadowing, declare a variable by the same name.