Rust Anti-Patterns Overview
Rust Anti-Patterns Overview
Using unwrap() and expect() in Production Code
Using unwrap() and expect() in Production Code
unwrap()
or expect()
can cause your program to panic. Use proper error handling with ?
operator and Result
types in production code.Overusing Clones
Overusing Clones
Not Using Iterators
Not Using Iterators
Using String When &str Would Suffice
Using String When &str Would Suffice
&str
for function parameters when you only need to read the string data, not own or modify it.Not Using Proper Error Types
Not Using Proper Error Types
Error
trait for better error handling and more context.Premature Optimization
Premature Optimization
Not Using Cargo Features
Not Using Cargo Features
Excessive Use of Unsafe
Excessive Use of Unsafe
unsafe
only when necessary and document why it’s needed. Prefer safe abstractions whenever possible.Not Using Lifetimes Correctly
Not Using Lifetimes Correctly
Not Using Rust's Type System
Not Using Rust's Type System
Not Using Option and Result
Not Using Option and Result
Option
for values that might be absent and Result
for operations that might fail, rather than sentinel values or exceptions.Ignoring Clippy Warnings
Ignoring Clippy Warnings
cargo clippy
regularly and address its warnings. Clippy catches many common mistakes and anti-patterns.Not Using Rust's Module System
Not Using Rust's Module System
Not Using Rust's Testing Framework
Not Using Rust's Testing Framework
#[test]
attributes for unit tests and integration tests.Not Using Cargo Workspaces
Not Using Cargo Workspaces
Not Using Rust's Concurrency Features Correctly
Not Using Rust's Concurrency Features Correctly
Not Using Cargo.lock Correctly
Not Using Cargo.lock Correctly
Not Using Rust's Documentation Features
Not Using Rust's Documentation Features
///
) to document your code, including examples that can be tested with cargo test --doc
.