How Do I … in Rust?
Discovery
… find a crate for a given purpose?
… find the latest version of a crate?
Project organization
… init a Rustmax project from template?
cargo generate brson/rustmax
… organize a Rust workspace?
Conveniences
… define "extension" methods on a type in another crate?
… guarantee a trait is object-safe?
static_assertions::assert_obj_safe!(MyTrait);
Error handling and debugging
… handle errors simply and correctly?
… structure errors in a public API?
… set up basic logging?
Collections
… create a fast HashMap?
… convert from slices to fixed-length arrays?
… Implement an impl Iterator with todo!?
fn merge_create_accounts_results(
accounts: &[tb::Account],
results: Vec<tb::CreateAccountsResult>,
) -> impl Iterator<Item = (u128, Option<tb::Account>)> + use<'_> {
todo!(); // optional
std::iter::empty() // satisfies the type checker
}
Numerics
… convert between numeric types ideomatically?
… perform math ideomatically?
… convert between ints and bytes?
… perform typical byte order conversions?
Since Rust 1.32, all integer types have built-in methods for byte order conversion:
#![allow(unused)] fn main() { // Convert integers to bytes let x: u32 = 0x12345678; let be_bytes = x.to_be_bytes(); // big-endian: [0x12, 0x34, 0x56, 0x78] let le_bytes = x.to_le_bytes(); // little-endian: [0x78, 0x56, 0x34, 0x12] let ne_bytes = x.to_ne_bytes(); // native-endian (platform-dependent) // Convert bytes back to integers let y = u32::from_be_bytes([0x12, 0x34, 0x56, 0x78]); // 0x12345678 let z = u32::from_le_bytes([0x78, 0x56, 0x34, 0x12]); // 0x12345678 }
These methods work on all integer types: u8, u16, u32, u64, u128, usize, and their signed equivalents.
Encoding, serialization, parsing
… serialize to and from JSON?
… decide what format to use with serde?
Time
… parse and render standard time formats?
Random numbers
… generate a strong random anything?
Use rand::random for convenience when you need cryptographically secure randomness without managing state.
#![allow(unused)] fn main() { let x: u32 = rand::random(); let y: f64 = rand::random(); // 0.0..1.0 }
… generate a strong random number from a seed?
Use rand::rngs::StdRng when you need reproducible cryptographically secure randomness. This uses the platform's secure RNG algorithm.
#![allow(unused)] fn main() { use rand::{Rng, SeedableRng}; let mut rng = rand::rngs::StdRng::seed_from_u64(42); let x: u32 = rng.gen(); }
… generate a fast random number from a seed?
Use rand::rngs::SmallRng for performance-critical code where cryptographic security isn't required. This automatically selects a fast algorithm.
#![allow(unused)] fn main() { use rand::{Rng, SeedableRng}; let mut rng = rand::rngs::SmallRng::seed_from_u64(42); let x: u32 = rng.gen(); }
… generate a strong random number from a seed with stable algorithm?
Use rand_chacha::ChaCha12Rng when you need reproducible results across Rust versions and platforms with cryptographic security.
#![allow(unused)] fn main() { use rand::{Rng, SeedableRng}; let mut rng = rand_chacha::ChaCha12Rng::seed_from_u64(42); let x: u32 = rng.gen(); }
… generate a fast random number from a seed with stable algorithm?
Use rand_pcg::Pcg64 for deterministic, fast random numbers that remain consistent across platforms and Rust versions.
#![allow(unused)] fn main() { use rand::{Rng, SeedableRng}; let mut rng = rand_pcg::Pcg64::seed_from_u64(42); let x: u32 = rng.gen(); }
Cryptography
… calculate a cryptographic content hash?
Parallelism and Concurrency
… initialize a global value?
todo LazyLock, OnceLock, and Once.
… send messages to/from async code?
todo futures::channels
… use a thread pool?
Use rayon::ThreadPool.
Although it has additional rayon features, it can be used as a basic thread pool.
todo example
Asynchronous I/O
… set up the tokio event loop?
… stub an unwritten async fn?
Networking and web
… make a synchronous HTTP request?
… configure a basic HTTP server?
Text / unicode
Terminal / CLI
… set up a simple CLI parser with subcommands?
… display colors in the terminal?
… read line-based input from the terminal?
… handle ctrl-?
Either use the [ctrl] crate or [tokio::signal::ctrlc].
todo say more w/ example