DEV Community

Cover image for Rust from the beginning, variables
Chris Noring for Microsoft Azure

Posted on • Updated on

Rust from the beginning, variables

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.

 Resources

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:

let account_balance = 4000;

println!("Your account balance {}", account_balance);
Enter fullscreen mode Exit fullscreen mode

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:

let name = "Chris";
Enter fullscreen mode Exit fullscreen mode

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:

let name = "Chris";
let age = 20;
Enter fullscreen mode Exit fullscreen mode

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:

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);
}
Enter fullscreen mode Exit fullscreen mode

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:

let account_balance = 4000;
Enter fullscreen mode Exit fullscreen mode

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

account_balance = 4500;
Enter fullscreen mode Exit fullscreen mode

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:

 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
Enter fullscreen mode Exit fullscreen mode

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:
   let mut account_balance = 4000;
   account_balance = 4500;
Enter fullscreen mode Exit fullscreen mode
  • 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:
   let account_balance = 4000;
   // other code
   let account_balance = 4500; // this version of account_balance shadows the first declared version.
Enter fullscreen mode Exit fullscreen mode

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:

const PI:f32 = 3.14
Enter fullscreen mode Exit fullscreen mode

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:

  |
6 |   const PI = 3.14;
  |         ^^ help: provide a type for the item: `PI: f64`
Enter fullscreen mode Exit fullscreen mode

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.

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.

Top comments (0)