Rust

Print

println!("a is equal to {}", a);

// Positional arguments
println!("arg0 = {0} arg1 = {1} arg0 = {0}", arg0, arg1);

// Named arguments
println!("arg0 = {first} arg1 = {second} arg0 = {first}", first = arg0, second arg1);

// Types
println!("binary {:b} hex {:x} octal {:o}", 10, 10, 10);

// Debugging
println!("{:?}", (12, true, "hello"));

Libraries

  • Library:
pub fn run() {
    // function
}
  • Main:
mod print;

fn main() {
    print::run();
}

Variables

// immutable 
let name = "Brad";

// mutable
let mut name = "Brad";

// const
const AGE: i32 = 23;

// multiple assign
let (name, age) = ("Brad", 37);

// Strings
let hello = String::from("Hello");

Strings

// length
hello.len()

// push
hello.push('W');
hello.push_str(". Another sentence.");

// methods
hello.capacity();
hello.is_empty();
hello.contains("World");
hello.replace("World", "There");

for word in hello.split_whitespace() {
    // code
}

// string with capacity
let mut s = String::with_capacity(10);

Assertions

assert_eq!(2, 3);

Arrays (fixed lists)

let numbers: [i32; 5] = [1, 2, 3, 4, 5];

// slice
let slice: &[i32] = &numbers[1..3];

Vectors (not fixed)

let mut numbers: Vec<i32> = vec![1, 2, 3, 4];

numbers.push(5);
numbers.pop();

for x in numbers.iter(){
    // code
}

// change values
for x in numbers.iter_mut(){
    *x = x + 2;
}

Types

// size of variable
std::mem::size_of_val(&numbers);

Conditionals

if a > 2 {
    // code
} else if a == 1 {
    // code
} else {
    // code
}

Loops

loop {
	// code
	if condition {
		break
	}
}

while condition {
	// code
}

for x in 0..100 {
	// code	
}

Switch statement (match)

match m {
	1 => println!("1!"),
	2 | 3 | 4 | 5 | 6 => println!("bigger than 1!"),
	_ => println!("what kind of dice is this"),
}

Functions

fn greeting(greet: &str, name: &str) {
	//code 
}

fn add(n1: i32, n2: i32) -> i32 {
	// no semi-colon means we return this!
	n1 + n2
}

Closures

// allow using variable outside of scope of normal function
let n3: i32 = 10;
let add_nums = |n1: i32, n2: i32| n1 + n2 + n3;
let result = add_nums(n1,n2);

Structs

struct Color {
	red: u8,
	green: u8,
	blue: u8
}

let mut c = Color {
	red: 255,
	green: 0,
	red: 0
}

println("Color red: {}", c.red);

Tuples

struct Color(u8, u8, u8);

let mut c = Color(255, 0, 0);
println("Color red: {}", c.1);

Functions on structs

impl Person {
	fn new(first: &str, last: &str) -> Person {
		Person {
			first_name: first.to_string(),
			last_name: last.to_string(),
		}
	}
	
	fn full_name(&self) -> String {
		format!("{} {}", self.first_name, self.last_name)
	}
	
	fn set_last_name(&mut self, last: &str) {
		self.last_name = last.to_string();
	}
}

let mut p = Person::new("John", "Doe");
println!("Person {} {}", p.first_name, p.last_name);