Skip to content
Shop

CommunityJoin Our PatreonDonate

Sponsored Ads

Sponsored Ads

Rust

Rust is a general-purpose programming language emphasizing performance, type safety, and concurrency. It enforces memory safety, meaning that all references point to valid memory, without a garbage collector.

Getting Started

Like Java, the main function is the entry point of a rust program.

rust
fn main() {
    println!("Hello, world!");
}

Rust must be compiled first and then ran.

bash
rustc main.rs && ./main
bash
rustc main.rs
.\main.exe

Comments

rust
// hello, world
rust
// So we’re doing something complicated here, long enough that we need
// multiple lines of comments to do it! Whew! Hopefully, this comment will
// explain what’s going on.

Data Types & Data Structures

LengthSignedUnsigned
8-biti8u8
16-biti16u16
32-biti32u32
64-biti64u64
128-biti128u128
archisizeusize

Compound Types

rust
fn main() {
    let tup: (i32, f64, u8) = (500, 6.4, 1);

    let tup = (500, 6.4, 1);
    let (x, y, z) = tup;
    println!("The value of y is: {y}");
}
rust
fn main() {
    let a = [1, 2, 3, 4, 5];
    let a: [i32; 5] = [1, 2, 3, 4, 5];
}

Learn more about Types

Variables

rust
fn main() {
    let x = 5;
    println!("The value of x is: {x}");
    x = 6;
    println!("The value of x is: {x}");
}
rust
fn main() {
    let mut x = 5;
    println!("The value of x is: {x}");
    x = 6;
    println!("The value of x is: {x}");
}
rust
fn main() {
    const THREE_HOURS_IN_SECONDS: u32 = 60 * 60 * 3;
}

Learn more about Variables

Statements

let

expression

Learn more about Statements

Control Flow

rust
fn main() {
    let number = 3;

    if number < 5 {
        println!("condition was true");
    } else {
        println!("condition was false");
    }
}

Learn more about Control Flow

Error Handling (Panic)

rust
fn main() {
    panic!("crash and burn");
}

Learn more about Error Handling

Loops

For Loop

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

    for text in v {
        println!("I like {}.", text);
    }
}
rust
fn main() {
    let mut sum = 0;
    for n in 1..11 {
        sum += n;
    }
    assert_eq!(sum, 55);
}
rust
fn main() {
    'a: loop {
        'a: loop {
            break 'a;
        }
        print!("outer loop");
        break 'a;
    }
}
rust
let result = 'block: {
    do_thing();
    if condition_not_met() {
        break 'block 1;
    }
    do_next_thing();
    if condition_not_met() {
        break 'block 2;
    }
    do_last_thing();
    3
};

Predicate Loop

rust
fn main(){
    let mut i = 0;
    while i < 10 {
        println!("hello");
        i = i + 1;
    }
}

Learn more about Loops

Functions

rust
fn main() {
    println!("Hello, world!");

    another_function();
}

fn another_function() {
    println!("Another function.");
}
rust
fn main() {
    print_labeled_measurement(5, 'h');
}

fn print_labeled_measurement(value: i32, unit_label: char) {
    println!("The measurement is: {value}{unit_label}");
}
rust
fn five() -> i32 {
    5
}

fn main() {
    let x = five();

    println!("The value of x is: {x}");
}

Learn more about Functions

Structs

rust
fn main(){
    struct Point {x: i32, y: i32}
    let p = Point {x: 10, y: 11};
    let px: i32 = p.x;
}
rust
fn main(){
    struct Cookie;
    let c = [Cookie, Cookie {}, Cookie, Cookie {}];
}

Learn more about Structs

Libraries

Package Management with Cargo

To check the Cargo version

bash
cargo --version

To create a new project

bash
cargo new hello_cargo
cd hello_cargo

Build the project

bash
cargo build

To run the project

bash
cargo run

Learn more about Cargo

Resources