.kiro/steering/rust-patterns.md
This file extends the common patterns with Rust specific content.
cargo fmt before committingcargo clippy -- -D warnings (treat warnings as errors)let by default; only let mut when mutation is required&T) by default; take ownership only when storing or consuming&str over String, &[T] over Vec<T> in function parameters// GOOD — borrows when ownership isn't needed
fn word_count(text: &str) -> usize {
text.split_whitespace().count()
}
// GOOD — takes ownership in constructor via Into
fn new(name: impl Into<String>) -> Self {
Self { name: name.into() }
}
Result<T, E> and ? for propagation — never unwrap() in production codethiserroranyhow for flexible error contextunwrap() / expect() for tests and truly unreachable states#[derive(Debug, thiserror::Error)]
pub enum ConfigError {
#[error("failed to read config: {0}")]
Io(#[from] std::io::Error),
#[error("invalid config format: {0}")]
Parse(String),
}
Prevent argument mix-ups with distinct wrapper types:
struct UserId(u64);
struct OrderId(u64);
fn get_order(user: UserId, order: OrderId) -> anyhow::Result<Order> {
todo!()
}
Model states as enums — make illegal states unrepresentable:
enum ConnectionState {
Disconnected,
Connecting { attempt: u32 },
Connected { session_id: String },
Failed { reason: String, retries: u32 },
}
Always match exhaustively — no wildcard _ for business-critical enums.
pub trait OrderRepository: Send + Sync {
fn find_by_id(&self, id: u64) -> Result<Option<Order>, StorageError>;
fn save(&self, order: &Order) -> Result<Order, StorageError>;
fn delete(&self, id: u64) -> Result<(), StorageError>;
}
std::env::var("API_KEY")unsafe blocks; every unsafe must have a // SAFETY: commentcargo audit and cargo deny check in CI#[cfg(test)] modules in the same filetests/ directoryrstest for parameterized tests, mockall for trait mockingcargo llvm-cov#[cfg(test)]
mod tests {
use super::*;
#[test]
fn creates_user_with_valid_email() {
let user = User::new("Alice", "[email protected]").unwrap();
assert_eq!(user.name, "Alice");
}
}
Organize by domain, not by type. Default to private; use pub(crate) for internal sharing.
See agents: rust-reviewer, rust-build-resolver for Rust-specific review and build error resolution.