Rust

Rust publishing and distribution

Install Rust and cargo

Maintenance and common operations

1
rustup update
1
cargo clean --help

Remove a crate with

1
cargo uninstall

Toolchains

1
2
rustup toolchain list --help
rustup component list -h

Other local documentation - books etc.

1
2
rustup doc --path --toolchain stable --clippy
rustup doc fn

Packages

Rust code is packaged as a collection of crates. It's possible to create private package registries.

Cargo honor locked versions, do a reproducible all binaries install.

1
cargo install --bins --locked
1
cargo install --list

Show package information with cargo info

Fearless Rust

Write the dumbest version first, optimise later (for multithreading - tokio, performance, optimal structures, cache etc). Clone data, don't bother with a reference (ignore lifetime issues). Waste RAM to get it working, worry later. Improvements are easy once you got it to compile the first time. Small changes easier.

Think of speed later, use for safety and type system.

Threads and borrowing references

Avoid immortal (static) references when writing asynchronous code. Allows borrowing references.

Wait for threads to complete, this happens at the end of a scope. Concurrency is a major goal of Rust, along with safety.

1
2
3
# Cargo.toml
[dependencies]
minreq = { version >= "2.14.1" }
1
2
3
4
5
6
7
8
9
// scope blocks until threads are done, therefore
// rustc has a guarantee that all threads will finish (join).

fn expensive_scope(url: &String) {
  std::thread::scope(|s| {
    s.spawn(|| minreq::get(url));
  })
  dbg!("url: {}", url);
}

Rust parallel asynchronous

Better not to use async unless threads is not enough.

tokio - most widely used async library, for network functions (need to wait for I/O). Muddies code, better to do without in the beginning. Consider a scoped tokio runtime.

std::sync::Arc - Atomically Reference Counted

smol is a small and fast async runtime.

rayon - converts iterators into parallel iterators. Rayon guarantees you that using Rayon APIs will not introduce data races.

Rust (mobile) app development

Tauri

Tauri uses web technologies, that means that virtually any frontend framework is compatible with Tauri.

Rust web development

Rust game development

Godot has Rust bindings.