Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Rust Language Basics

Variables

Rust contains many of the same types of variables used in other programming languages. These variables include:

You will notice that, unlike higher-level languages like Python, Rust has many different types of integers. The first letter indicates the sign (u for unsigned, i for signed), and the number indicates the number of bits. Therefore, u8 is an unsigned 8-bit integer, i32 is a signed 32-bit integer, and so on. usize is a special type that is the same size as the memory address of the computer, and is used for indexing into arrays and vectors.

By default, variables are immutable, which means that once you assign a value you cannot modify (mutate) it anymore. However, you have the option to make your variables mutable by adding mut in front of the variable name.

When defining a variable, sometimes the compiler can infer the type.

let x = 14;

Other times it cannot and requires you provide the type

let x: u8 = 14;

Read more in the Rust Book

Functions

Functions in Rust are defined as:

fn main() {
    //expression;
}

Functions are private by default. To make them public, add pub before fn. A function can be called from another file if it is public.

pub fn main() {
    //expression;
}

Functions can have a return type that is indicated with -> after the name. For example fn square(n: u64) -> u64

The return can be explicitly given with return [expression or variable]; or it can be the last line of a function with no semicolon:

fn square(n: u64) -> u64 {
    return n * n;
}

or

fn square(n: u64) -> u64 {
    n * n
}

Read more in the Rust Book

Control Flow

if Expressions:

fn main() {
    let number = 10;
    
    if number % 5 == 0 {
        println!("number is divisble by 5");
    } else {
        println!("number is not divisble by 5");
    }
}

loop

fn main() {
    let mut counter = 0;

    loop { 
        counter += 1
        if counter == 10 {
            break counter;       // will break the loop and return counter value
        }
    }
}

while conditional loop

fn main() {
    let mut number = 5;
    while number > 0 {
        println!(number);
        number -= 1
    }
}

for loop

fn main() {
    let v = &["apples", "cake", "coffee"];

    for text in v {
        println!("I like {}.", text);
    }
}

You can also iterate through a series of integers.

fn main() {
    let mut sum = 0;
    for n in 1..11 {
        sum += n;
    }
    assert_eq!(sum, 55);
}

Rust Macros

Rust has a powerful macro system that consists of two major types of macros:

You have already seen declarative macros in println!, assert_eq!. They are built into the language and support metaprogramming, which allows you to write more condensed code that expands into more verbose code at compile time. Macros are defined for functions such as println! to allow for variable arguments, a language feature that is not possible with plain functions in Rust.

Procedural macros are more advanced; you will see a version of them in the Object-Oriented Features module.

Read more in the Rust Book