Rust

Rust Overview

Install: curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh

"Hello, World!" program

Filename: main.rs

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

Creating a Project with Cargo

cargo new hello_cargo

cd hello_cargo

Packages and Crates

Building a Cargo Project

cargo build

Building and Running a Cargo Project

cargo run

Checks a Cargo Project

cargo check

Building for Release

cargo build --release

Variables and Mutability

Filename: src/main.rs

fn main() {
let mut x = 5;
println!("The value of x is: {x}");
x = 6;
println!("The value of x is: {x}");
let x = 7;
println!("The value of x is: {x}");
}

Constants

Filename: src/main.rs

#![allow(unused)]
fn main() {
const THREE_HOURS_IN_SECONDS: u32 = 60 * 60 * 3;
}

Shadowing

Filename: src/main.rs

fn main() {
let x = 5;
let x = x + 1;
{
let x = x * 2;
println!("The value of x in the inner scope is: {x}");
}
println!("The value of x is: {x}");
}

Filename: src/main.rs

fn main() {
let spaces = " ";
let spaces = spaces.len();
}

Data Types

Floating-Point Types

Filename: src/main.rs

#![allow(unused)]
fn main() {
let x = 2.0;
let y: f32 = 3.0;
}

Integer Types

Filename: src/main.rs

fn main() {
let x: i64 = 100000;
let y: i64 = 150000;
let result: i64 = x * y;
println!("The value 150K * 100K = {result}");
}

Numeric Operations

Filename: main.rs

fn main() {
let sum = 5 + 10;
let difference = 95.5 - 4.3;
let product = 4 * 30;
let quotient = 56.7 / 32.2;
let truncated = -5 / 3;
let remainder = 43 % 5;
}

The Boolean Type

Filename: main.rs

fn main() {
let t = true;
let f: bool = false;
}

The Character Type

Filename: main.rs

fn main() {
let c = 'z';
let z: char = 'ℤ';
let heart_eyed_cat = '😻';
}

The Tuple Type

Filename: main.rs

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

Filename: main.rs

fn main() {
let tup = (500, 6.4, 1);
let (x, y, z) = tup;
println!("The value of y is: {y}");
}

Filename: main.rs

fn main() {
let x: (i32, f64, u8) = (500, 6.4, 1);
let five_hundred = x.0;
let six_point_four = x.1;
let one = x.2;
}

The Array Type

Filename: main.rs

fn main() {
let a = [1, 2, 3, 4, 5];
}

Filename: main.rs

fn main() {
let months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
}

Filename: main.rs

fn main() {
let a: [i32; 5] = [1, 2, 3, 4, 5];
}

Filename: main.rs

fn main() {
let a = [3; 5];
}

Filename: main.rs

fn main() {
let a = [1, 2, 3, 4, 5];
let first = a[0];
let second = a[1];
}

Functions

Filename: main.rs

fn main() {
println!("Hello, world!");
another_function();
}
fn another_function() {
println!("Another function.");
}

Parameters

Filename: main.rs

fn main() {
another_function(5);
}
fn another_function(x: i32) {
println!("The value of x is: {x}");
}

Filename: main.rs

fn main() {
print_labeled_measurement(5, 'h');
}
fn print_labeled_measurement(value: i32, unit_label: char) {
println!("The measurement is: {value}{unit_label}");
}

Statements and Expressions

Filename: main.rs

fn main() {
let y = 6;
}

Filename: main.rs

fn main() {
let y = {
let x = 3;
x + 1
};
println!("The value of y is: {y}");
}

Functions with Return Values

Filename: main.rs

fn five() -> i32 {
5
}
fn main() {
let x = five();
println!("The value of x is: {x}");
}

Filename: main.rs

fn main() {
let y = {
let x = 3;
x + 1
};
println!("The value of y is: {y}");
}

Filename: main.rs

fn main() {
let x = plus_one(5);
println!("The value of x is: {x}");
};
fn plus_one(x: i32) -> i32 {
x + 1
}

Comments

Filename: main.rs

fn main() {
// hello, world
}

Filename: main.rs

fn main() {
// 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.
}

Filename: main.rs

fn main() {
let lucky_number = 7; // I'm feeling lucky today
}

Filename: main.rs

fn main() {
// I'm feeling lucky today
let lucky_number = 7;
}

Control flow

If

Filename: main.rs

fn main() {
let number = 3;
if number < 5 {
println!("Condition was true");
} else {
println!("Condition was false");
}

Loop

Filename: src/main.rs

fn main() {
loop {
println!("Condition was true");
}
}

Returning Values from Loops

Filename: src/main.rs

fn main() {
let mut counter = 0;
let result = loop {
counter += 1;
if counter == 10 {
break counter * 2;
}
};
println!("The result is {result}");
}

While

Filename: src/main.rs

fn main() {
let mut number = 3;
while number != 0 {
println!("Condition was true");
number -= 1;
}
println!("LIFTOFF!!!");
}

Filename: src/main.rs

fn main() {
let a = [10, 20, 30, 40, 50];
let mut index = 0;
while index < 5 {
println!("the value is: {}", a[index]);
index += 1;
}
}

For

Filename: src/main.rs

fn main() {
let a = [10, 20, 30, 40, 50];
for element in a {
println!("the value is: {element}");
}
}

Filename: src/main.rs

fn main() {
for number in (1..4).rev() {
println!("{number}!");
}
println!("LIFTOFF!!!");
}