Rust tips
5 tips · newest first
Rust tips on async, traits, pattern matching, and API design — idioms that make code compile-time safe and future-proof.
Pass inline async handlers with async closures and the AsyncFn traits (Rust 1.85)
RUSTRust 1.85 stabilized async closures (async || { ... }) and the AsyncFn/AsyncFnMut/AsyncFnOnce traits. These let higher-…
rust1.85async
@hnoozUse native async fn in traits and drop the async-trait crate
RUSTSince Rust 1.75, async fn works directly in traits (via return-position impl Trait), giving you static dispatch with ze…
rust1.75async
@hnoozFlatten guard clauses with let ... else
RUSTlet ... else binds a pattern or diverges — the else block must return, break, continue, or panic, so control flow leave…
rustpattern-matching
@hnoozFuture-proof public types with
RUST#[non_exhaustive] on a public enum or struct forces downstream crates to include a wildcard arm when matching, and bloc…
rustapi-design
@hnoozControl impl Trait lifetime capture precisely with use<> (Rust 2024)
RUSTIn the Rust 2024 edition, return-position impl Trait captures every in-scope generic and lifetime by default. That's us…
rust2024impl-trait
@hnooz