Rust: Learning Notes

Created: | Last updated:

Terminology (as it compares to JS)

  • Crates = Modules in Rust, similar to npm modules or go modules
    • As they explain it, Crates are “packages of code”
  • cargo = Rust’s build system and package manager, or their npm equivalent
  • Standard Library = The default library that ships with Rust, like the native modules that come with Node. This can actually disabled so it does not get included in the binary.
    • This sdlib depends on the system where the binary will run since it interacts with lower level stuff (like most higher level languages)
  • Cargo.toml = package.json

Useful Commands to Know

  • cargo new = similar to npm init
  • rustfmt = similar to gofmt, formats your code
    • “The Rust team has included this tool with the standard Rust distribution, as rustc  is, so it should already be installed on your computer!”

Syntax Examples

// Iterate N times
for i in 0..num {
// do something
}

// Create an array of 100 elements with value 1
let hundredElements = [1; 100];

// an array
let a = [1, 2, 3, 4]

// as vector
let a = vec![1, 2, 3, 4]

Learnings

  • A call with ! means you are calling a macro
  • There are 2 types of “string” = “string” and “char”
  • Arrays & Vectors: Arrays are fixed length, vectors are not
  • Confusing because of JS's let:
    • let is UNMUTABLE
    • let mut is MUTABLE
  • Shadowing, or redeclaring the same variable name resulting in a new variable
  • Ownership
    • Stack vs Heap
    • Simpler, predictable-in-size data types will use stacks, the other ones will use heap
    • There are many implications from this, such as:
      • Scope owning a variable
      • Concept of moving a variable
  • Pointers:
    • *i and &i
  • Structs
    • Unit-like = empty structs
    • Tuple struct
    • Classic struct

Interesting Resources

Rustling - Rust Exercises Writing an OS in Rust in tiny steps (Steps 1-5)