Rust Standards
Rust Standards
Section titled “Rust Standards”1. Package Management
Section titled “1. Package Management”- Tool:
cargo.Cargo.tomlfor dependencies. - Version: Rust 1.70+ (use latest stable). Pin the exact toolchain in
rust-toolchain.tomland keeprust-versionand CI in step with it — see §21.4. - Lock Files:
Cargo.lockcommitted for applications, not libraries. - Age Gate: Do not adopt any crate version published less than 3 days ago. Verify publish date via crates.io API before upgrading. See
sec-01§5 for exceptions.
2. Code Style
Section titled “2. Code Style”- Formatter:
rustfmt. Run viamake fmtorcargo fmt. - Linter:
clippy. Lints are declared once in[workspace.lints]at levelwarn; CI promotes them withcargo clippy --workspace --all-targets --all-features -- -D warnings(§21.3). Enforced bylint-checks/rust/clippy-ci. - Warnings: Treated as errors in CI, via
-D warningson the command line — not via#![deny(warnings)]in source. A source-level deny turns every new rustc/clippy lint into a build break for every consumer on their next toolchain upgrade; the CLI flag keeps the gate where it belongs.
rustfmt.toml
Section titled “rustfmt.toml”edition = "2021"max_width = 100tab_spaces = 43. Naming Conventions
Section titled “3. Naming Conventions”- Files:
snake_case.rs - Modules:
snake_case - Types/Structs/Enums:
PascalCase - Functions/Variables:
snake_case - Constants:
UPPER_SNAKE_CASE - Lifetimes: Single lowercase letter:
'a,'b
4. Project Structure
Section titled “4. Project Structure”src/├── domain/│ ├── entities.rs│ └── value_objects.rs├── application/│ ├── use_cases.rs│ └── interfaces.rs└── infrastructure/ └── repositories.rs5. Language Features
Section titled “5. Language Features”- Ownership: Understand ownership, borrowing, and lifetimes. Prefer borrowing over cloning.
- Error Handling: Use
Result<T, E>for fallible operations. Use?operator for propagation. - Option: Use
Option<T>for nullable values. Prefer overnull/Nonepatterns. - Pattern Matching: Use
matchandif letfor exhaustive pattern matching.
Example
Section titled “Example”#[derive(Debug, Clone, PartialEq, Eq)]pub struct Email { value: String,}
impl Email { pub fn new(value: String) -> Result<Self, InvalidEmailError> { if !value.contains('@') || !value.contains('.') { return Err(InvalidEmailError(value)); } Ok(Email { value }) }
pub fn as_str(&self) -> &str { &self.value }}
#[derive(Debug, thiserror::Error)]#[error("Invalid email: {0}")]pub struct InvalidEmailError(String);6. Error Handling
Section titled “6. Error Handling”- Error Types: Use
thiserrorfor library errors,anyhowfor application errors. - Result: Use
Result<T, E>for all fallible operations. Never use panics for expected errors. - Propagation: Use
?operator for error propagation. Handle errors at boundaries.
use thiserror::Error;
#[derive(Error, Debug)]pub enum DomainError { #[error("Invalid email: {0}")] InvalidEmail(String),
#[error("User not found: {0}")] UserNotFound(String),
#[error("Database error: {0}")] Database(#[from] sqlx::Error),}7. Testing
Section titled “7. Testing”- Framework: Built-in
#[test]and#[cfg(test)]. Usecargo test. - Mocking: Use
mockallfor trait mocking. Use test doubles for integration tests. - Coverage: Use
cargo-tarpaulinorgrcov. 95% is the absolute minimum for any module. Target 100% for domain, 95%+ for application and infrastructure.
Test Structure
Section titled “Test Structure”#[cfg(test)]mod tests { use super::*;
#[test] fn should_create_user_with_valid_email() { // Given
// When let user = User::new(email, "Test User".to_string());
// Then }}8. Async/Await
Section titled “8. Async/Await”- Runtime: Use
tokioorasync-stdfor async runtime. - Futures: Use
async/awaitsyntax. UseFuturetrait for custom futures. - Error Handling: Use
Result<T, E>with async functions. Handle errors with?.
use tokio;
async fn fetch_user(id: &str) -> Result<User, DomainError> { let user = user_repository .find_by_id(id) .await? .ok_or_else(|| DomainError::UserNotFound(id.to_string()))?; Ok(user)}9. Dependencies
Section titled “9. Dependencies”Common Crates
Section titled “Common Crates”- HTTP:
reqwest(client),axumoractix-web(server) - Database:
sqlx(async SQL),diesel(ORM),sea-orm(async ORM) - Serialization:
serdewithserde_json - Error Handling:
thiserror,anyhow - Logging:
tracingwithtracing-subscriber
10. Documentation
Section titled “10. Documentation”- Doc Comments: Use
///for public items,//!for module-level docs. - Format: Use Markdown. Include examples with
#for hidden code.
/// Creates a new user with validated email address.////// # Arguments////// * `email` - Valid email address (must match RFC 5322)/// * `name` - User's full name (non-null, non-empty)////// # Returns////// New `User` entity instance.////// # Errors////// Returns `InvalidEmailError` if email format is invalid.////// # Examples////// ```rust/// # use domain::email::Email;/// let email = Email::new("[email protected]".to_string())?;/// let user = User::new(email, "John Doe".to_string());/// ```pub fn create_user(email: Email, name: String) -> User { // Implementation}11. Memory Safety
Section titled “11. Memory Safety”- Ownership: Understand ownership rules. Use references (
&) to avoid moving. - Lifetimes: Use lifetime parameters (
'a) when necessary. Prefer higher-ranked trait bounds. - Unsafe: Avoid
unsafecode unless absolutely necessary. Document why unsafe is needed.
12. Performance
Section titled “12. Performance”- Zero-Cost Abstractions: Leverage Rust’s zero-cost abstractions (iterators, closures).
- Allocation: Minimize allocations. Use
&stroverStringwhen possible. - Profiling: Use
cargo flamegraphorperffor performance profiling.
13. Cargo Features
Section titled “13. Cargo Features”- Feature Flags: Use
[features]inCargo.tomlfor optional dependencies. - Workspaces: Use workspaces for monorepo structure.
- Publishing: Follow semantic versioning. Use
cargo publishfor crates.io.
14. Inline Test Modules
Section titled “14. Inline Test Modules”- Convention: Tests live in
#[cfg(test)] mod tests { use super::*; ... }within each source file. - Rationale: Inline tests have access to private helpers without polluting the public API. Refactoring code and tests together is easier when they’re in the same file.
- Exception: Integration tests that need compiled binaries or external processes belong in
tests/at the crate root.
// In src/document.rs#[cfg(test)]mod tests { use super::*;
#[test] fn insert_at_boundary_extends_document() { let mut doc = Document::new("hello"); doc.insert(5, " world").unwrap(); assert_eq!(doc.text(), "hello world"); }}15. Workspace Member Tooling
Section titled “15. Workspace Member Tooling”- Convention: Data generators, seed tools, and scaffolding belong as workspace members, not external scripts.
- Rationale: Ensures version consistency with the main crate. CI can build and test them alongside the app. They’re easy to iterate on during development.
- Example: A
tools/my-seed/crate that generates test fixtures is a workspace member in the rootCargo.toml:
[workspace]members = [ "packages/*", "apps/*", "tools/my-seed",]16. Feature Flag Conventions
Section titled “16. Feature Flag Conventions”- Named flags for optional capabilities:
cosmic-shaping = ["dep:cosmic-text"]. - Reserved flags for documented future work:
experimental-sync = []with a comment explaining intent. - Documentation: Always add a comment in
Cargo.tomlexplaining what a feature flag enables.
[features]# Enable advanced text shaping via cosmic-textcosmic-shaping = ["dep:cosmic-text"]# Reserved for future CRDT-based collaboration (automerge-rs)experimental-sync = []17. Enforcing Documentation
Section titled “17. Enforcing Documentation”- Library crates: Add
#![deny(missing_docs)]tolib.rs. Every public item requires a///doc comment. - App crates: Documentation is recommended but not enforced. App entry points change frequently and internal APIs are less stable.
- Compiler warnings as errors: Enforced in CI with
-D warnings(§2, §21.3), not with#![deny(warnings)]in crate roots. The only source-leveldenyattributes are the targeted ones:missing_docson libraries andclippy::disallowed_methodson FFI/wasm facades (§21.2).
18. Error Handling Layering (Rust-Specific)
Section titled “18. Error Handling Layering (Rust-Specific)”- Library crates: Use
thiserrorwith typed error enums. Each error variant includes context fields. - App crates: Use
anyhowfor context-rich error chaining. Wrap library errors with.context("what was happening"). - GUI event handlers: Handlers that return
()(common in frameworks like Makepad, egui, iced) shouldeprintln!errors, neverpanic!orunwrap().
// Library crate#[derive(Debug, thiserror::Error)]pub enum DocumentError { #[error("offset {offset} is out of bounds for document length {len}")] OffsetOutOfBounds { offset: usize, len: usize },}
// App crateuse anyhow::Context;fn load_project(path: &Path) -> anyhow::Result<Project> { let data = std::fs::read_to_string(path) .context("failed to read project file")?; // ...}19. Workspace Cargo Configuration
Section titled “19. Workspace Cargo Configuration”.cargo/config.toml: Use for workspace-wide settings (target-specific flags, custom runners, env vars).RUSTC_WRAPPER: When external build tools hard-codeRUSTFLAGS(e.g.,cargo-makepad wasm build), use a wrapper script to inject required flags without conflicting.- Target-specific config: Platform-specific compilation flags belong in
.cargo/config.toml, not scattered across Makefiles.
[target.wasm32-unknown-unknown]rustflags = ['--cfg', 'getrandom_backend="custom"']20. Security
Section titled “20. Security”Full security standards:
standards/security/sec-01_security_standards.md
- SAST: Run
cargo clippy --all-targets --all-features -- -D warningsin CI. Usecargo-geigerto auditunsafeusage across dependencies. - Dependency scanning: Run
cargo auditandcargo deny checkin CI. - SAST: Run
cargo clippy -- -D warningsandcargo geigerin CI for static analysis and unsafe code auditing. - Banned functions: See sec-01_security_standards.md for the complete banned-functions list with language-specific examples.
- Unsafe code: Minimize
unsafeblocks. Document safety invariants for everyunsafeusage. Prefer safe abstractions. - Secure random: Use
rand::rngs::OsRngorgetrandomcrate for cryptographic randomness.
21. FFI & wasm Facades
Section titled “21. FFI & wasm Facades”A facade module is the thin layer that exposes Rust to another runtime: extern "C" functions in src/ffi.rs (consumed from Swift, C, Python, …) or #[wasm_bindgen] exports in src/wasm.rs. Everything the host can call lives there, and nothing else in the crate knows the host exists. The facade has one job — translate between the host’s ABI and the crate’s typed API — and it has stricter rules than the rest of the crate because a mistake there is undefined behavior or a host-process crash, not a Rust panic with a backtrace.
21.1 Panics never cross the boundary
Section titled “21.1 Panics never cross the boundary”- A panic that unwinds out of an
extern "C"function aborts the process (Rust 1.81+) or is undefined behavior (earlier). In wasm a panic traps, which the JS host sees as an opaqueRuntimeErrorwith the Rust stack gone. - The facade therefore never calls anything that can panic on a reachable input: no
unwrap/expect, nopanic!/unreachable!/todo!, no slice indexing (buf[i]→buf.get(i)), no unchecked arithmetic on host-supplied lengths. - Every
Resultis converted at the facade:extern "C"returns a status code (0success, negative errno-style codes) and writes outputs through out-pointers; wasm exports returnResult<T, JsError>(orResult<T, JsValue>) so the host gets a real exception with a message. - As a last line of defence, wrap the body of every
extern "C"function instd::panic::catch_unwindand map a caught panic to an error code. This is belt-and-braces: the lint in §21.2 is the real gate,catch_unwindjust turns a bug into a reportable error instead of an abort.
#![deny(clippy::disallowed_methods)]#![deny(clippy::indexing_slicing, clippy::panic, clippy::unwrap_used, clippy::expect_used)]
use std::panic::{catch_unwind, AssertUnwindSafe};
#[repr(i32)]pub enum Status { Ok = 0, NullPointer = -1, Parse = -2, Internal = -99 }
/// # Safety/// `bytes` must point to `len` readable bytes, or be null (returns NullPointer).#[no_mangle]pub unsafe extern "C" fn mesh_parse(bytes: *const u8, len: usize, out: *mut *mut Mesh) -> i32 { let result = catch_unwind(AssertUnwindSafe(|| { if bytes.is_null() || out.is_null() { return Status::NullPointer; } // SAFETY: caller guarantees `bytes` points to `len` readable bytes (see `# Safety`). // Explicit block: `unsafe_op_in_unsafe_fn` warns otherwise, and CI runs `-D warnings`. let slice = unsafe { std::slice::from_raw_parts(bytes, len) }; match crate::parse(slice) { Ok(mesh) => { // SAFETY: `out` is non-null (checked above) and the caller guarantees it is writable. unsafe { *out = Box::into_raw(Box::new(mesh)) }; Status::Ok } Err(_) => Status::Parse, } })); result.unwrap_or(Status::Internal) as i32 // allowed: Result<Status, Box<dyn Any>> from catch_unwind, never panics}#![deny(clippy::disallowed_methods)]use wasm_bindgen::prelude::*;
#[wasm_bindgen]pub fn parse_mesh(bytes: &[u8]) -> Result<JsValue, JsError> { let mesh = crate::parse(bytes).map_err(|e| JsError::new(&e.to_string()))?; serde_wasm_bindgen::to_value(&mesh).map_err(|e| JsError::new(&e.to_string()))}21.2 The disallowed-methods pattern
Section titled “21.2 The disallowed-methods pattern”Clippy’s unwrap_used/expect_used are blunt across a whole crate — tests and internal invariants legitimately unwrap. Scope the ban to the facade instead:
- List the methods once, with reasons, in
clippy.tomlat the workspace root.disallowed-methodstakes fully qualified paths, so the list is precise and self-documenting. - Declare the lint at
warnworkspace-wide in[workspace.lints.clippy]so every crate sees it locally without failing the build. - Promote it to
denyonly in the facade modules with a module-level#![deny(clippy::disallowed_methods)]at the top ofsrc/ffi.rs/src/wasm.rs. Those files fail to compile undercargo clippyif anyone adds anunwrap; the rest of the crate just gets a warning that CI turns into an error (§21.3). - Grep gate in
lint-standards:lint-checks/rust/ffi-no-unwrapscansffi.rs,wasm.rs,*_ffi.rs,*_wasm.rs, andffi//wasm/directories forunwrap/expect/panic!-family calls, so the rule holds even when clippy is not installed on the reviewer’s machine.
disallowed-methods = [ { path = "core::option::Option::unwrap", reason = "panics cross the FFI/wasm boundary as UB — return an error code" }, { path = "core::option::Option::expect", reason = "same as unwrap" }, { path = "core::result::Result::unwrap", reason = "same as unwrap" }, { path = "core::result::Result::expect", reason = "same as unwrap" },]# Cargo.toml (workspace root)[workspace.lints.clippy]disallowed_methods = "warn" # facades promote to deny with #![deny(...)]unwrap_used = "warn"expect_used = "warn"indexing_slicing = "warn"
[workspace.lints.rust]unsafe_op_in_unsafe_fn = "warn"Every member crate opts in with [lints] workspace = true.
21.3 Warn locally, deny in CI
Section titled “21.3 Warn locally, deny in CI”- Local:
cargo clippyprints warnings and exits 0. Developers see every finding without a red build while iterating. - CI:
cargo clippy --workspace --all-targets --all-features -- -D warningsmakes the same findings fatal. Run one clippy lane per meaningful feature set (e.g.--no-default-features,--features wasm,--all-features) — feature-gated code is only linted when its feature is on. - Never put
#![deny(warnings)]in source (§2). It makes the crate’s build depend on which toolchain the consumer happens to have. - Enforced by
lint-checks/rust/clippy-ci, which looks for acargo clippy … -D warnings(orRUSTFLAGS=-D warnings) step in CI.
21.4 Toolchain pin consistency
Section titled “21.4 Toolchain pin consistency”Three places name the compiler, and they drift independently unless something checks them:
| File | Key | Role |
|---|---|---|
rust-toolchain.toml |
channel = "1.99.0" |
What cargo actually uses locally and in CI (rustup honours it) |
Cargo.toml |
rust-version = "1.99" |
MSRV: the oldest compiler that must build the crate; cargo refuses older toolchains |
| CI workflow | dtolnay/[email protected], toolchain: 1.99.0, rustup default 1.99.0 |
What CI installs when it does not read the file |
Rules:
rust-toolchain.tomlis the source of truth. Pin an exactmajor.minor.patch;stableis not a pin.- Applications:
rust-versionequals the pinned toolchain’smajor.minor. Bump both (andCargo.lock) in onechore(rust): upgrade toolchain to X.Y.Zcommit, with an ADR entry if the bump changes edition or MSRV policy. - Libraries may declare an older MSRV than the pinned toolchain, but then CI must have a lane that builds on
rust-version(dtolnay/rust-toolchain@<msrv>), or the MSRV is a fiction. - CI either omits an explicit toolchain (so rustup reads the file) or pins the identical version. Pinning a different explicit version is an error.
rust-versionnewer than the pinned toolchain is always wrong (the pinned compiler cannot build the crate).- Enforced by
lint-checks/rust/toolchain-pin: FAIL on a CI pin orrust-versionthat contradictsrust-toolchain.toml; WARN when the file is missing, floats, orrust-versiontrails it.
21.5 License allow-list (deny.toml)
Section titled “21.5 License allow-list (deny.toml)”- Every workspace ships a
deny.tomlwith a[licenses] allow = [...]list of SPDX identifiers;cargo deny checkruns in CI alongsidecargo audit. An allow-list (not a deny-list) means a new copyleft dependency fails the build until someone consciously adds its license. - Keep
[advisories]on the current cargo-deny schema: vulnerabilities are always errors, so the knobs areunmaintained = "workspace"andyanked = "deny"(the oldvulnerability = "deny"/unmaintained = "warn"keys were removed and now fail to parse).[bans] multiple-versions = "warn", and[sources]restricted to crates.io plus explicitly listed git sources. - Enforced by
lint-checks/rust/deny-config: FAIL when nodeny.toml, WARN when it has no non-emptyallowlist.
[licenses]allow = ["MIT", "Apache-2.0", "Apache-2.0 WITH LLVM-exception", "BSD-2-Clause", "BSD-3-Clause", "ISC", "Unicode-3.0", "Zlib"]confidence-threshold = 0.9
[advisories]# Vulnerabilities always fail; these are the remaining knobs on the current schema.unmaintained = "workspace"yanked = "deny"
[bans]multiple-versions = "warn"wildcards = "deny"
[sources]unknown-registry = "deny"unknown-git = "deny"