Cursor rule
.cursor/rules/rust-coding-style.mdcCursor rules
Quality
78/100
Scores the file, not the repository.Length
560 words
10 headings · 5 code blocksRepository
6
— · pushed 80 days agoLast changed
3 days ago
First indexed 3 days ago.12345# Rust Coding Style67> This file extends [common/coding-style.md](../common/coding-style.md) with Rust-specific content.89## Formatting1011- **rustfmt** for enforcement — always run `cargo fmt` before committing12- **clippy** for lints — `cargo clippy -- -D warnings` (treat warnings as errors)13- 4-space indent (rustfmt default)14- Max line width: 100 characters (rustfmt default)1516## Immutability1718Rust variables are immutable by default — embrace this:1920- Use `let` by default; only use `let mut` when mutation is required21- Prefer returning new values over mutating in place22- Use `Cow<'_, T>` when a function may or may not need to allocate2324```rust25use std::borrow::Cow;2627// GOOD — immutable by default, new value returned28fn normalize(input: &str) -> Cow<'_, str> {29 if input.contains(' ') {30 Cow::Owned(input.replace(' ', "_"))31 } else {32 Cow::Borrowed(input)33 }34}3536// BAD — unnecessary mutation37fn normalize_bad(input: &mut String) {38 *input = input.replace(' ', "_");39}40```4142## Naming4344Follow standard Rust conventions:45- `snake_case` for functions, methods, variables, modules, crates46- `PascalCase` (UpperCamelCase) for types, traits, enums, type parameters47- `SCREAMING_SNAKE_CASE` for constants and statics48- Lifetimes: short lowercase (`'a`, `'de`) — descriptive names for complex cases (`'input`)4950## Ownership and Borrowing5152- Borrow (`&T`) by default; take ownership only when you need to store or consume53- Never clone to satisfy the borrow checker without understanding the root cause54- Accept `&str` over `String`, `&[T]` over `Vec<T>` in function parameters55- Use `impl Into<String>` for constructors that need to own a `String`5657```rust58// GOOD — borrows when ownership isn't needed59fn word_count(text: &str) -> usize {60 text.split_whitespace().count()61}6263// GOOD — takes ownership in constructor via Into64fn new(name: impl Into<String>) -> Self {65 Self { name: name.into() }66}6768// BAD — takes String when &str suffices69fn word_count_bad(text: String) -> usize {70 text.split_whitespace().count()71}72```7374## Error Handling7576- Use `Result<T, E>` and `?` for propagation — never `unwrap()` in production code77- **Libraries**: define typed errors with `thiserror`78- **Applications**: use `anyhow` for flexible error context79- Add context with `.with_context(|| format!("failed to ..."))?`80- Reserve `unwrap()` / `expect()` for tests and truly unreachable states8182```rust83// GOOD — library error with thiserror84#[derive(Debug, thiserror::Error)]85pub enum ConfigError {86 #[error("failed to read config: {0}")]87 Io(#[from] std::io::Error),88 #[error("invalid config format: {0}")]89 Parse(String),90}9192// GOOD — application error with anyhow93use anyhow::Context;9495fn load_config(path: &str) -> anyhow::Result<Config> {96 let content = std::fs::read_to_string(path)97 .with_context(|| format!("failed to read {path}"))?;98 toml::from_str(&content)99 .with_context(|| format!("failed to parse {path}"))100}101```102103## Iterators Over Loops104105Prefer iterator chains for transformations; use loops for complex control flow:106107```rust108// GOOD — declarative and composable109let active_emails: Vec<&str> = users.iter()110 .filter(|u| u.is_active)111 .map(|u| u.email.as_str())112 .collect();113114// GOOD — loop for complex logic with early returns115for user in &users {116 if let Some(verified) = verify_email(&user.email)? {117 send_welcome(&verified)?;118 }119}120```121122## Module Organization123124Organize by domain, not by type:125126```text127src/128├── main.rs129├── lib.rs130├── auth/ # Domain module131│ ├── mod.rs132│ ├── token.rs133│ └── middleware.rs134├── orders/ # Domain module135│ ├── mod.rs136│ ├── model.rs137│ └── service.rs138└── db/ # Infrastructure139 ├── mod.rs140 └── pool.rs141```142143## Visibility144145- Default to private; use `pub(crate)` for internal sharing146- Only mark `pub` what is part of the crate's public API147- Re-export public API from `lib.rs`148149## References150151See skill: `rust-patterns` for comprehensive Rust idioms and patterns.152
Also in ThanhTrunggDEV/DontBeLazy
Diff this repo’s formatsOne repository carrying more than one format is the comparison this product exists for: does anyone actually write different content in each file, or is one a copy of the other?
| Repository | Format | Stack | Covers | Score | Changed |
|---|---|---|---|---|---|
| ThanhTrunggDEV/DontBeLazy.cursor/rules/zh-agents.mdc · 6 | Cursor rules | no sections | 50/100 | 3 days ago | |
| ThanhTrunggDEV/DontBeLazy.cursor/rules/zh-patterns.mdc · 6 | Cursor rules | api | 30/100 | 3 days ago | |
| ThanhTrunggDEV/DontBeLazy.agent/AGENTS.md · 6 | AGENTS.md | buildteststylearch+4 | 77/100 | 3 days ago | |
| ThanhTrunggDEV/DontBeLazy.cursor/AGENTS.md · 6 | AGENTS.md | buildteststylearch+4 | 77/100 | 3 days ago | |
| ThanhTrunggDEV/DontBeLazy.cursor/rules/common-agents.mdc · 6 | Cursor rules | agent-behaviour | 50/100 | 3 days ago | |
| ThanhTrunggDEV/DontBeLazy.cursor/rules/common-code-review.mdc · 6 | Cursor rules | styletesting-strategygitsecurity+3 | 65/100 | 3 days ago | |
| ThanhTrunggDEV/DontBeLazy.cursor/rules/common-coding-style.mdc · 6 | Cursor rules | style | 54/100 | 3 days ago | |
| ThanhTrunggDEV/DontBeLazy.cursor/rules/common-development-workflow.mdc · 6 | Cursor rules | gitagent-behaviour | 39/100 | 3 days ago | |
| ThanhTrunggDEV/DontBeLazy.cursor/rules/common-git-workflow.mdc · 6 | Cursor rules | lint-formatgitagent-behaviour | 43/100 | 3 days ago | |
| ThanhTrunggDEV/DontBeLazy.cursor/rules/common-hooks.mdc · 6 | Cursor rules | styletypessecuritydo-not | 36/100 | 3 days ago | |
| ThanhTrunggDEV/DontBeLazy.cursor/rules/common-patterns.mdc · 6 | Cursor rules | lint-formatstyleapi | 52/100 | 3 days ago | |
| ThanhTrunggDEV/DontBeLazy.cursor/rules/common-performance.mdc · 6 | Cursor rules | buildperformance | 48/100 | 3 days ago | |
| ThanhTrunggDEV/DontBeLazy.cursor/rules/common-security.mdc · 6 | Cursor rules | security | 39/100 | 3 days ago | |
| ThanhTrunggDEV/DontBeLazy.cursor/rules/common-testing.mdc · 6 | Cursor rules | testtesting-strategyagent-behaviour | 34/100 | 3 days ago | |
| ThanhTrunggDEV/DontBeLazy.cursor/rules/cpp-coding-style.mdc · 6 | Cursor rules | lint-formatstyle | 52/100 | 3 days ago | |
| ThanhTrunggDEV/DontBeLazy.cursor/rules/cpp-hooks.mdc · 6 | Cursor rules | buildlint-formatdeployment | 60/100 | 3 days ago | |
| ThanhTrunggDEV/DontBeLazy.cursor/rules/cpp-patterns.mdc · 6 | Cursor rules | style | 54/100 | 3 days ago | |
| ThanhTrunggDEV/DontBeLazy.cursor/rules/cpp-security.mdc · 6 | Cursor rules | securityperformancedo-not | 73/100 | 3 days ago | |
| ThanhTrunggDEV/DontBeLazy.cursor/rules/cpp-testing.mdc · 6 | Cursor rules | testtesting-strategy | 55/100 | 3 days ago | |
| ThanhTrunggDEV/DontBeLazy.cursor/rules/csharp-coding-style.mdc · 6 | Cursor rules | lint-formatstyletypes | 66/100 | 3 days ago |
Diff against .cursor/rules/zh-agents.mdc Diff against .cursor/rules/zh-patterns.mdc Diff against .agent/AGENTS.md Diff against .cursor/AGENTS.md Diff against .cursor/rules/common-agents.mdc Diff against .cursor/rules/common-code-review.mdc Diff against .cursor/rules/common-coding-style.mdc Diff against .cursor/rules/common-development-workflow.mdc Diff against .cursor/rules/common-git-workflow.mdc Diff against .cursor/rules/common-hooks.mdc Diff against .cursor/rules/common-patterns.mdc Diff against .cursor/rules/common-performance.mdc Diff against .cursor/rules/common-security.mdc Diff against .cursor/rules/common-testing.mdc Diff against .cursor/rules/cpp-coding-style.mdc Diff against .cursor/rules/cpp-hooks.mdc Diff against .cursor/rules/cpp-patterns.mdc Diff against .cursor/rules/cpp-security.mdc Diff against .cursor/rules/cpp-testing.mdc Diff against .cursor/rules/csharp-coding-style.mdc
Similar configs
Same format, overlapping stack, ranked by quality.
| Repository | Format | Stack | Covers | Score | Changed |
|---|---|---|---|---|---|
| hiromaily/go-crypto-wallet.cursor/rules/typescript.mdc · 126 | Cursor rules | setupbuildtestlint-format+6 | 100/100 | 3 days ago | |
| TechSquidTV/Hermes.cursor/rules/10-hermes-api.mdc · 45 | Cursor rules | testlint-formatstylearch+5 | 100/100 | 3 days ago | |
| markstev/mark-starter.cursor/rules/frontend.mdc · 0 | Cursor rules | setuptestlint-formatstyle+6 | 99/100 | 3 days ago | |
| Allymahmoud/case-intake-platform.cursor/rules/frontend.mdc · 0 | Cursor rules | setuptestlint-formatstyle+7 | 99/100 | 3 days ago | |
| deifos/clipmira-subtitles.cursor/rules/frontend.mdc · 1 | Cursor rules | setuptestlint-formatstyle+7 | 99/100 | 3 days ago | |
| dodgecfr/combatfilms-webapp.cursor/rules/frontend.mdc · 0 | Cursor rules | setuptestlint-formatstyle+7 | 99/100 | 3 days ago | |
| langflow-ai/langflow.cursor/rules/docs_development.mdc · 153k | Cursor rules | setupbuildtestlint-format+7 | 97/100 | 3 days ago | |
| TechSquidTV/Hermes.cursor/rules/20-hermes-api-tests.mdc · 45 | Cursor rules | teststyletesting-strategysecurity+3 | 97/100 | 3 days ago |
