Rust101: Variables

Mukundh Bhushan
4 min readFeb 8, 2020

--

In the previous article, we wrote our first “Hello, World” program in rust. Now let's dive into variables.

TL;DR

Type of variables
Datatypes
Defining variables

First, let’s start off by creating a new project called “variables” using the following command

cargo new variables --bin

Change directory to the project folder, Navigate to the main.rs file and follow along…

Types of variable

Rust offers us two types of variables that we can declare. mutable and non-mutable.

Non-mutable variables

As its name suggests the values of a mutable variable can not be changed during the execution of the program.

Mutable variables

In mutable variables values can be changed during the execution of the program.

Data types in Rust

Rust offers the following Primitive data types:

  • Integers: u8, i8, u16, i16, u32, i32, u64, i64, u128, i128
  • Floats: f32, f64
  • Boolean (bool)
  • Characters (char)
  • Tuples
  • Arrays

The numbers in integer and float data types denote the memory the variable takes.

Basic differences between Arrays and Tuple in Rust

Arrays in Rust

  • collection of values of the same data types
  • fixed-sized collection
  • Elements are accessed using the index which starts from 0.

We will be going into greater details on arrays in the later articles for now here is how an array looks like

Notice the “[ ]” brackets being used and all the elements are of the same datatypes.

Tuples in rust

  • collection of values of different types
  • finite heterogeneous sequence
  • Elements are accessed using the “ . ” operator followed by the index of the element.

We will be going into greater details on tuples in the later articles for now here is how an array looks like

Notice the “( )” brackets being used and the elements can be of different data types.

Ways we can declare variables

We can declare variables in two ways:

  • Implicit
  • Explicit

Implicit

An implicit declaration is when you don’t specify the datatype while defining the variable.

Here is the syntax for defining variables implicitly

let <var name> = <value>;

Let's implicitly define an integer variable

During an implicit variable declaration, Rust chooses the default datatype which suits best. The variable “a” defaults to “i32”

Here is the list of defaults:

  • Integer: i32
  • Float: f64
  • if reserved words like “true” or “false” are used. then bool
  • Strings: &str

Explicit

An Explicit declaration is when you specify the datatype while defining the variable.

Here is the syntax for defining variables Explicit

let <var name>:<datatype> = <value>;

Let’s explicitly define an integer variable

Variable “a” in this case is an “i64” although “i32” would do the trick.

Defining variables

Defining integers and floats variables

Defining boolean variables

Defining tuples and arrays the easy way

There is more when it comes to tuples and arrays. For now, let's learn the easy ways to define them.

You must have noticed the array and tuple defined above are explicitly defined. Implicit definitions is when they get harder.

Defining strings and characters the easy way

Rust has a lot to offer when it comes to Strings and its inbuilt functions. For now, let’s learn the easy ways to define them.

Just like arrays and tuples explicitly defining strings is easier. Implicit definitions is when they get harder. Implicitly defining strings are stored as &str based pointer variable or a “string slice” which is a substring of the main string instead of an independent string.

Defining emojis

Rust allows you to use emojis as well with the help of Unicodes. Here is the list of all the Unicode for all the popular emojis.

In the next article, we will discuss about mutability in variables.

link to the next article

Link to the previous article

--

--