Skip to content

Clippy & rustfmt — Linting and Formatting

In TypeScript projects you typically wire up two separate tools: eslint for catching logical mistakes and enforcing code patterns, and prettier for opinionated formatting. Each needs its own config, its own plugins, and sometimes they conflict.

Rust ships two first-party tools that are always present:

TypeScriptRustPurpose
eslintcargo clippyCatches bugs, anti-patterns, and non-idiomatic code
prettiercargo fmtEnforces a single canonical style
.eslintrcclippy.toml or #[allow(...)]Configuration
.prettierrcrustfmt.tomlFormatting overrides
Terminal window
# TypeScript
npx prettier --write "src/**/*.ts"
# Rust
cargo fmt # format all files in the workspace
cargo fmt --check # CI mode: exit 1 if any file would change

cargo fmt wraps rustfmt. The defaults are the official style and almost no one overrides them. A minimal rustfmt.toml at the project root can adjust a few options:

# rustfmt.toml (optional — most projects have none)
max_width = 100
edition = "2021"
Terminal window
# TypeScript
npx eslint src/
# Rust
cargo clippy # warn on lint violations
cargo clippy -- -D warnings # treat every warning as a compile error (use on CI)
TypeScript
// eslint might warn: prefer const
let items = getItems();
// eslint might warn: no-unused-vars
function helper(x: number, _y: number) {
return x * 2;
}
Rust
// Clippy warns: this could be written more idiomatically
let v: Vec<i32> = vec![1, 2, 3];
let doubled: Vec<i32> = v.iter().map(|x| x * 2).collect();
// Clippy warns: use .is_empty() instead of .len() == 0
if doubled.len() == 0 {
println!("empty");
}
// Idiomatic version:
if doubled.is_empty() {
println!("empty");
}
// Suppress one lint on the next item
#[allow(clippy::needless_pass_by_value)]
fn process(data: Vec<u8>) { /* ... */ }
// Suppress across the whole file (put at crate root)
#![allow(dead_code)]

Run both tools on CI so formatting and lint errors are caught before merge:

Terminal window
cargo fmt --check
cargo clippy -- -D warnings
cargo test

These are bash commands to run in your terminal or CI pipeline — there is no runnable browser snippet for linter invocations.

Which Cargo subcommand formats your Rust source files (equivalent to Prettier)?
What flag do you pass to `cargo clippy` to make every warning a hard error (useful on CI)?
How do you suppress a single Clippy lint on one function without disabling it globally?