rust

i've started learning rust, and it's been fun and eye-opening. below are just my notes from the docs, or from other resources.

cargo

  • build system and package manager

create cargo

cargo new < cargo_name >

build, run or check cargo

cargo build
  • produces an exec file
cargo run
  • compiles and run exec in one cmd
cargo check
  • checks to make sure code still compiles, doesn't produce exec

cargo lock

  • cargo lock ensures reproducibility - locks dependencies versions

variables and mutability

  • to make a variable mutable use mut after let

  • to make a constant use const <var_name>: <data_type> = ...

  • use ALL CAPS for const (naming convention)

shadowing

  • a variable can be shadowed by the second (same var name)
  • have to use let again.
  • using let means that we can do transformation on that variable, then it becomes immutable again.

data types

  • scalar types: represents single value

    • integers, floating-point nums, booleans and characters
    • 8-bit: i8, u8
    • 16-bit: i16, u16
    • 32-bit: i32, u32
    • 64-bit: i64, u64
    • 128-bit: i128, u128,
    • arch: isize, usize
  • integer overflow: rust will "panic" at runtime if overflow occurs.to handle possibility of overflow you can wrap all modes with wrapping_* methods, return None if overflow using checked_* method, return val and boolean indicating whether there was overflow using overflowing_* methods, or saturate the value's min or max values with saturing_* methods

  • floating point types: represents num with decimal values

    • f32 or f64
  • booleans: true, false

  • char: use single quotes ''

compounds types

  • tuples: let tup: (<data type>, <data type>, <data type>, ...) = (...,...,...)

    • access tuples using tup.0, tup.<index>
  • array: arrays have fixed length in rust

    • access arrays using a[<index>]

functions

fn main() {
 
    another_function();
 
    let num1 = 5;
 
    second_function(num1);
 
    let y = {
        let x = 3;
        x + 1
    };
 
    println!("val of y = {y}");
}
 
fn another_function() {
    println!("Another function!")
}
 
fn second_function(x: i32) {
    println!("Value of x is {x}");
}
  • this gives "Another function", "Value of x is 5", and y=4
  • notice how the inside the block there is no seimicolon, if there is a semicolon it becomes a statement, and wont return a value.

functions with return values

fn second_function(x: i32) {
    println!("Value of x is {x}");
}
 
fn five() -> i32 {
    5
}
  • the above has no parameters and defines the type of return value, no semicolon as we don't want it to be a statement, but rather a return value

control flow

  • use if, else like other languages
  • but can also do same line (same data type) let if
  • three types of loops: loop, while and for
    • loop is infinitely running, unless told to stop
    • a loop is to retry operation that might fail, like checking if a thread has completed its job
  • you can also have loop labels (what the heck!)
fn main() {
    let number = 3;
 
    // standard if else if else
    if number < 10 {
        println!("less than 10")
    } else if number > 15 {
        println!("more than 15")
    } else {
        println!("hullo");
    }
 
    // if with else and let
    let condition = true;
    let number2 = if condition { 5 } else { 6 };
    println!("{number2}");
    let mut counter = 0;
 
    // loop
    let result = loop {
        counter += 1;
 
        if counter == 10 {
            break counter * 2;
        }
    };
 
    println!("The result is {result}");
 
    // labeled loop
    let mut count = 0;
    'counting_up: loop {
        println!("count = {count}");
        let mut remaining = 10;
 
        loop {
            println!("remaining = {remaining}");
            if remaining == 9 {
                break;
            }
            if count == 2 {
                break 'counting_up;
            }
            remaining -= 1;
        }
 
        count += 1;
    }
    println!("End count = {count}");
 
    // while loop
    let mut number = 10;
 
    while number != 0 {
        number -= 1;
    }
 
    // for loop in elems
 
    let a = [10, 20, 30, 40, 50];
 
    for element in a {
        println!("the value is: {element}");
    }
}

citations: https://doc.rust-lang.org/book/

On this page