- https://rust-lang-nursery.github.io/rust-cookbook/
- stdx
- https://blog.burntsushi.net/rust-error-handling/
Mantras
- Invalid State Should Not Be Represented (hence the builder pattern)
&str, String, &[u8] Vec<u8> conversion map
&str -> String | String::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] -> &str | s.to_vec()or s.to_owned() |
&[u8] -> String | std::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> -> String | std::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
- Chip 8 emulator with WebAssembly
- https://tobiasvl.github.io/blog/write-a-chip-8-emulator/
- http://devernay.free.fr/hacks/chip8/C8TECH10.HTM
- https://github.com/ColinEberhardt/wasm-rust-chip8/tree/master/src
WGPU
- WGSL spec (w3c)
- Pipelines and buffers
- Engines example
Are We Web Yet?
Analysis Tools For Rust
- speedscope, flamegraph etc (rust profiling)
- hotspot, firefox profiler reads perf files
Iterator Sheet
- .cloned() is cool, into iter etc https://hermanradtke.com/2015/06/22/effectively-using-iterators-in-rust.html
- itertools
- https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.enumerate
Futures
Lyon: SVG to mesh thing
Integration for physics basics (and the whole site)
Learning
- https://rustwasm.github.io/book/
- https://github.com/ctjhoa/rust-learning
- https://pragprog.com/book/khrust/programming-webassembly-with-rust
- https://www.amazon.com/Hands-Functional-Programming-Rust-applications-ebook/dp/B07C5S4729
- https://www.amazon.com/Hands-Concurrency-Rust-Confidently-memory-safe/dp/1788399978
- CryptoPals, like PngMe but for Crypto
- Crust of Rust, Async
- Let’s build a browser engine
- Learning Rust In 2020
- Common Rust Lifetime Misconceptions
Exercises
- Exercism
- AOC2018
- AOC2019
Write Good Code
- Rust Patterns and AntiPatterns mdBook
- to_owned to express the intent, string::from otherwise
- Rust Style Guidelines
- Common Rust Lifetime Misconceptions
- Rust API Guidelines (good for idiomatic)
- Idiomatic Rust
- https://github.com/ferrous-systems/elements-of-rust
- https://cheats.rs/
- Slice pattern maching
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
- Pretty Assertions
- https://crates.io/crates/splines (for our drawing needs)
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
TODO apprendre les trait trics a la bevy
eg audiostate<XChannel> multiple channels kira audio