1 - Intro

Last edited

Goal of Rust: Safety

Rust programs are free from:

  • Dangling pointers - Live references to data that has become invalid over the course of the program
  • Data races
  • Buffer overflow - An attempt to access the 12th element of an array with only 6 elements
  • Iterator invalidation - An issue caused by something that is iterated over after being altered midway through
  • Integer overflow (debug mode only) - when integers hit their limit and flow over to the beginning

Anonymous

If you’ve ever programmed in a dynamic language, then you may have encountered the frustration of your program crashing because of a misnamed variable. Rust brings that frustration forward so that your users don’t have to experience the frustration of things crashing.

Important

A buffer is a space set aside in memory for receiving input. Data can leak from one read to the next if the buffer’s contents are not cleared between writes.

Why does this situation occur? Programmers hunt for performance. Buffers are reused to minimize how often memory applications ask for memory from the OS.

Where does Rust fit best?

  • Command-line utilities
    • minimal startup time, low memory use, and easy deployment
    • Utilities written in Rust are compiled as static binaries by default.
  • Data processing
    • Rust excels at text processing and other forms of data wrangling
    • Rust has the fastest regex engine
    • Used for search engines, data-processing engines and log-parsing.
    • small filter programs can be easily embedded into the larger framework
  • Extending applications
    • Well suited ot extend programs written in dynamic languages.
    • Sentry, a company that processes application errors, uses Rust for cpu intense parts of their Python system.
  • Resource-constrained environments
    • C has occupied the domain of microcontrollers for decades.
    • Any input parsing code will be routinely probed for weaknesses
    • Rust can play an important role here by adding a layer of safety without imposing runtime costs
  • Server-side applications
    • Most applications written in Rust live on the server
    • Rust is used to write databases, monitoring systems, search appliances, and messaging systems
  • Desktop
  • Mobile
    • Rust is able to talk to the phone via the same interface with no additional runtime cost.
  • Systems programming
    • duh… the purpose of rust.

CSV parsing example (snippets)

Create a vector (slice in Go)

Vec<_>

  • Vec is short for _vector_ = slice in Go
  • <_> usually a type isbetween the < >, _ means infer the type of the elements
let fields: Vec<_> = record
    .split(',')
    .map(|field| field.trim())
    .collect();

Print to stderr

println! stdout
eprintln! stderr

  • {} ~ __str__
  • {:?} ~ __repr__
println!("{}, {}cm", name, length);
eprintln!("debug: {:?} -> {:?}", record, fields);

Walrus assignment and type conversion

// Attempt to parse fields[1] as a 32-bit floating-point number
// if that is successful, then assign the number to the length variable.
if let Ok(length) = fields[1].parse::<f32>() {
    println!("{}, {}cm", name, length);
}

Debug mode

Use this macro for debugging, rust won’t compile the code guarded by it, it’s basically just the convetion for debugging mode.

if cfg!(debug_assertions) {
    eprintln!("debug: {:?} -> {:?}", record, fields); // won't compile!
}

cargo run --release