Char


let character = 'a';

chars are single unicode scalars that are denoted by single quotes.

they are not interchangeable like in other non-typed languages

chars are not a single byte but 4 bytes in Rust.

Arrays


A fixed-size list of elements of the same type

by default are immutable but you can still use mut

let m = [1, 2, 3, 4];
 
let mut n = [1, 2, 3, 4];

shorthand for initializing an array filled with zeros.

let list = [0; 20];
 
// list; [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

get length; println!("list length: {}", list.len());

slices


are portions of data structures, and are a safe way to reference certain parts of data.

slices can be immutable or mutable

let a = [1, 2, 3, 4, 5, 6];
 
let a_slice = &a[2..5];
 
// a_slice has only 2, 3, 4, 5 inside it. 

very similar to a reference.

Str


is the primitive string type for Rust but it’s an unsized type but is useful once you use it as a reference; &str.

Strings

Tuples


Ordered list of fixed size

let tuple: (i32, &str) = (1, "hello world");

you can access values of a tuple through deconstruction;

let (x, y, z) = ("Powder", "Puff", "Girls");

IF statements


something cool you can do with Rust is this

let y = if x > 10 {
	5
} else {
	4
};
 
// or 
 
if z = if y < 5 { 3 } else { 2 };

other than that Rust uses “else if” like everybody else.

loops


  1. loop
  2. while
  3. for

the loop keyword is the simplest loop in Rust and is an infinite loop.

while loop works the same way as most other languages.

let mut x = 5;
 
let mut done: bool = false;
 
while !done {
	x += x - 3;
 
	println!("{}", x);
 
	if x % 5 == 0 {
		done = true;
	}
}

For loop


for loop does not have a “c-style” for loop, best thing we have is something like this:

for x in 0..10 {
	println!("{}", x);
}
 
 
 
// general idea
for var in expression {
	code
}
 
 
//if you need the index, you can enumerate like Python
 
for (index, value) in (0..10).enumerate() {
	println!("index: {}, value: {}", index, value);
}

you can also use break to stop iteration early

you can also label loops:

 
#![allow(unused_variables)]
fn main() {
'outer: for x in 0..10 {
    'inner: for y in 0..10 {
        if x % 2 == 0 { continue 'outer; } // Continues the loop over `x`.
        if y % 2 == 0 { continue 'inner; } // Continues the loop over `y`.
        println!("x: {}, y: {}", x, y);
    }
}
}