Rust Ecosystem
Core Concepts
- Ownership: Each value has one owner; when owner goes out of scope, value is dropped
- Borrowing:
&Tallows multiple immutable borrows;&mut Tallows exactly one; cannot mix - Result/Option:
Result<T, E>for recoverable errors,Option<T>for optional values; use?for propagation - Traits: Define behavior with traits; use
derivefor common implementations
Borrowing Rules
&T- multiple simultaneous immutable borrows allowed&mut T- exactly one mutable borrow allowed- Cannot have
&mut Twhile&Texists
Error Handling
// Custom error with thiserror
#[derive(Debug, thiserror::Error)]
enum MyError {
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("Parse error: {msg}")]
Parse { msg: String },
}
// Propagate with ?
fn read_config() -> Result<Config, MyError> {
let content = std::fs::read_to_string("config.toml")?;
Ok(parse(content)?)
}
Common Traits
Clone- explicit.clone();Copy- implicit bitwise copyDebug-{:?}formatting;Display-{}formattingDefault- default value constructionPartialEq/Eq,PartialOrd/Ord- comparisonFrom/Into- type conversionsHash- for HashMap/HashSet keys
Project Structure
Cargo.toml
src/
lib.rs # Library crate root
main.rs # Binary crate root
bin/ # Additional binaries
tests/ # Integration tests
benches/ # Benchmarks
examples/ # Example code
Cargo.toml
[package]
name = "my-crate"
version = "0.1.0"
edition = "2021"
rust-version = "1.83"
[dependencies]
serde = { version = "1.0", features = ["derive"] }
[lints.clippy]
pedantic = "warn"
unwrap_used = "deny"
[profile.release]
lto = true
codegen-units = 1
strip = true
Workspace
[workspace]
resolver = "2"
members = ["crate-a", "crate-b"]
[workspace.dependencies]
serde = "1.0"
tokio = { version = "1", features = ["full"] }
Member crates inherit with serde.workspace = true.
Anti-Patterns
- unwrap() in library code: Use
?or proper error handling - Clone abuse: Prefer borrowing when possible
- String for everything: Use enums, newtypes for domain modeling
- Arc<Mutex> overuse: Consider channels or ownership patterns first
Tools
cargo check- Fast syntax/type checkcargo clippy -- -D warnings- Lintercargo fmt- Formattercargo nextest run- Better test runnercargo audit- Security vulnerability scanningcargo tree- Dependency tree
rustfmt.toml
edition = "2021"
max_width = 100
imports_granularity = "Crate"
group_imports = "StdExternalCrate"
Context7 Reference
- Rust Book:
/rust-lang/book - Cargo:
/rust-lang/cargo.git - Clippy:
/rust-lang/rust-clippy
Converted and distributed by TomeVault — claim your Tome and manage your conversions.