Optimization: use RUSTFLAGS="-C target-cpu=native" to take advantage of CPU special features.


For local rust/std documentation, do rustup doc.

Little tricks

Run tests with stdout output:

cargo test -- --nocapture

To run tests with logging enabled (eg, with env_logger), make sure you add env_logger::init() to the test function itself.

map() and Result Ergonomics

.collect() has some magical features! In addition to turning an iterator of Item into Vec<Item>, it will turn an iterator of Result<Item> into Result<Vec<Item>>. This makes it really useful for the end of functions.

This is particularly useful for resolving some categories of “error handling in map closures”: you can use ? in the map closure as long as you wrap the happy path with Ok() and call collect on the outside. Eg:

let list: Vec<Item> = junk
    .map(|thing| Ok(Item {
        a: thing.a,
        b: fixup(thing.widget)?,

What about when map over an Option? Eg:

let toy = Shiny {
    a: 123,
    b:|v| paint(v).expect("paint to succeed"),

Should use match in this case:

let toy = Shiny {
    a: 123,
    b: match component {
        None => None,
        Some(v) => Some(paint(v)?),