Rust is a systems programming language focused on safety, speed, and concurrency. It enforces memory safety without using garbage collection, making it ideal for performance-critical applications.
Rust Anti-Patterns Overview
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
Not Using Iterators
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
Error
trait for better error handling and more context.Premature Optimization
Not Using Cargo Features
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 Rust's Type System
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
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 Testing Framework
#[test]
attributes for unit tests and integration tests.Not Using Cargo Workspaces
Not Using Rust's Concurrency Features Correctly
Not Using Cargo.lock Correctly
Not Using Rust's Documentation Features
///
) to document your code, including examples that can be tested with cargo test --doc
.