Mantras

&str, String, &[u8] Vec<u8> conversion map

&str -> StringString::from(s) or s.to_string() or s.to_owned()
&str -> &[u8]s.as_bytes()
&str -> Vec<u8>s.as_bytes().to_vec() or s.as_bytes().to_owned()
String -> &str&s if possible* else s.as_str()
String -> &[u8]s.as_bytes()
String -> Vec<u8>s.into_bytes()
&[u8] -> &strs.to_vec()or s.to_owned()
&[u8] -> Stringstd::str::from_utf8(s).unwrap(), but don’t**
&[u8] -> Vec<u8>String::from_utf8(s).unwrap(), but don’t**
Vec<u8> -> &str&s if possible* else s.as_slice()
Vec<u8> -> Stringstd::str::from_utf8(&s).unwrap(), but don’t**
Vec<u8> -> &[u8]String::from_utf8(s).unwrap(), but don’t**

*target should have explicit type (i.e., checker can’t infer that)

**handle the error properly instead

CHIP-8

WGPU

Are We Web Yet?

Analysis Tools For Rust

  • speedscope, flamegraph etc (rust profiling)
  • hotspot, firefox profiler reads perf files

Iterator Sheet

Futures

Lyon: SVG to mesh thing

Integration for physics basics (and the whole site)

Learning

Exercises

Write Good Code

fn is_palindrome(items: &[char]) -> bool {
    match items {
        [first, middle @ .., last] => first == last && is_palindrome(middle),
        [] | [_] => true,
    }
}
    let point = [3.14, -42.0];
    let [x, y] = point;

Anonymous lifetime

Use when possible

fn (x: &'x str, y: &'y str) -> &'y str {
// Instead do (etc)
fn(x: &str, y: &'_ str) -> &'_ str {

ref vs &

ref = MAKE a new reference

// doesn't work
Some(&mut remainder) = self.remainder;
// Would match Option<&mut T>

Some(ref mut remainder) = self.remainder;
// Matches Option<T> and then makes a mut ref
==
Some(remainder) = &mut self.remainder;
//Equivalent

as_ref()

let text: Option<String> = Some("Hello, world!".to_string());
// First, cast `Option<String>` to `Option<&String>` with `as_ref`,
// then consume *that* with `map`, leaving `text` on the stack.
let text_length: Option<usize> = text.as_ref().map(|s| s.len());
println!("still can print text: {:?}", text);

Cow, copy on write

Write an OS In Rust

Flutter rust bridge

Making slow rust code fast

Packages

Rust API Guidelines

Tokio

  • When you spawn a task on the Tokio runtime, its type’s lifetime must be ‘static. This means that the spawned task must not contain any references to data owned outside the task.
  • Tasks spawned by tokio::spawn must implement Send. This allows the Tokio runtime to move the tasks between threads while they are suspended at an .await.
  • There are a couple of different ways to share state in Tokio. Guard the shared state with a Mutex. Spawn a task to manage the state and use message passing to operate on it.
  • Throughout Tokio, the term handle is used to reference a value that provides access to some shared state.

TODO Snippets (order this)

  • p (println)
  • no (None)
  • b (block)
  • let
  • ma/match (match)
  • enum
  • impl
  • struct (st)
  • x => y (=>)
  • for in (for)
  • return (ret)
  • function (fn)
  • use
  • vec![] (v)
  • some (so)
  • =
  • letm
  • ifl
  • panic (pa)
  • result (res)
  • assert (ass)
  • else
  • eprintln (ep)
  • if
  • loop
  • derive (der)
  • fileopen (fo)
  • new (new)
  • asyncfn (afn)
  • test
  • format (f)
  • pubfn (pfn)
  • string from (sf)
  • trait (tr)
  • type
  • whilel
  • match?
  • vec with cap (vwc)
  • pafn
  • asse
  • assn

Stuff from exercism

  • slice window
  • .chain( && std::iter::once
  • if in the middle of match arms

The Rust Performance Book

  • return iterators instead of vectors if you’re gonna extend, or iterate again after, avoids allocations

https://rust-for-rustaceans.com/ appendix for Rust for Rustaceans

TODO new rustlings!! https://github.com/rust-lang/rustlings/releases/tag/5.0.0

My ideal Rust workflow

https://rustwasm.github.io/docs/wasm-bindgen/examples/dom.html

Ravenslofty/prussia: Prussia - a Rust PS2 SDK.

aarch64-switch-rs

aarch64-switch-rs/cargo-nx: Cargo subcommand to simplify building Nintendo Switch projects

Secrecy crate, for secret values

Dotenv, both runtime and compile time envs (great with secrecy)

The “Any” Trait

Learn Rust The Dangerous Way

Ports a benchmark of planets from C to Rust