Loading...
Loading...
Google's Rust style guide for coding agents: paste-ready rustfmt.toml, clippy.toml and [lints], the judgement rules no tool checks (module layout, imports, API shape, panics, naming), and where Google overrides common Rust advice. Use when writing or reviewing Rust to Google style, or setting up a Rust crate's lint config.
npx skill4agent add 0xmassi/google-rust-style-skill google-rust-style2ac1584G#anchorhttps://google.github.io/styleguide/rustguide.html#anchorthiserrorcargo clippyrust_librarychromium::import!asyncstd::threadfuchsia_sync::Mutexcargo clippy --all-targets --all-features -- -D warningswarnrustfmt.tomlmax_width = 100imports_granularity = "Module"
hex_literal_case = "Lower"
format_macro_matchers = trueclippy.tomlavoid-breaking-exported-api = falsewrong_self_conventionref_optionpubavoid-breaking-exported-api = false
allow-unwrap-in-tests = true
disallowed-methods = [
{ path = "std::panic::catch_unwind", reason = "Google Rust style forbids catch_unwind in first-party code" },
{ path = "std::sync::PoisonError::into_inner", reason = "Google Rust style: never reach inside a poisoned mutex" },
{ path = "std::sync::PoisonError::get_ref", reason = "Google Rust style: never reach inside a poisoned mutex" },
{ path = "std::sync::PoisonError::get_mut", reason = "Google Rust style: never reach inside a poisoned mutex" },
]Cargo.tomlunsafe_code = "forbid"[lints.rust]
unsafe_code = "forbid"
unstable_features = "deny"
[lints.clippy]
ref_option = "deny"
unwrap_used = "deny"
wildcard_imports = "deny"
needless_for_each = "deny"
undocumented_unsafe_blocks = "deny"
derive_partial_eq_without_eq = "allow"
missing_const_for_fn = "allow"[lints.rust]
missing_docs = "warn"
[lints.clippy]
exhaustive_enums = "deny"
exhaustive_structs = "deny"
must_use_candidate = "deny"
return_self_not_must_use = "deny"
missing_panics_doc = "deny"#[expect(<lint>, reason = "...")]reason| Setting | Google rule | Section |
|---|---|---|
| A | G#use-statement |
| Lowercase hex literals; formatted macro matchers | G#formatting-other |
| No glob imports. It already exempts Google's allowed cases: | G#glob-imports |
| Take | G#flexible-references |
default | | G#known-traits |
default | Derive when a derive can express it | G#derive |
default | A type with | G#important-traits |
| Prefer | G#important-traits |
default | | G#naming-methods |
default | | G#expected-api-sets |
| | G#catch-unwind |
| Never reach inside a poisoned mutex | G#poison |
| Public enums and all-pub-field structs used by other teams are | G#non-exhaustive-enums, G#non-exhaustive-structs |
| | G#must-use-on-functions, G#must-use-on-types |
default | Pattern-match instead of check-then-unwrap | G#dont-check-then-unwrap |
| | G#expect-over-unwrap |
| A | G#combinator-chains |
| Unsafe code is isolated in one crate, each block carries | G#unsafe, G#writing-doc |
| Document public APIs and every panic on malformed input | G#writing-doc |
| No | G#unstable-features |
| A public fn is | G#const-functions |
non_exhaustive_omitted_patternsFoomod foofoo.rsmodmod tests#[cxx::bridge]use std::mem;mem::swap(..)use regex::Regex;Regexio::ErrorIoErrorhir#[rustfmt::skip]\u{200D}RcArcimpl IteratorVec<T>VecVecimpl IntoIterator&[T]impl Into<T>Tboolenumboolconnect(addr, /* retry= */ true)Optionchecked_addResultpanic!ResultDayMonthYearimpl Traitdyn TraitimplFooExtFooDateLikeCopyCopyCopyClonePartialEqPartialOrd==<EqOrdHashEqOrdFromnew()from_foo()DefaultOpts { a: 1, ..Default::default() }#[non_exhaustive]#[expect]exhaustive_enumsexhaustive_structsColorRectangle#[non_exhaustive]Shape::Triangle | _ => {}if letlet elsemacro_rules!proc_macro'_for<'a>R: ReadTUVec<T>new()new_with_foo()try_new()Resultfrom_foo()Fromfoo()foo_mut()set_foo(v)take_foo()replace_foo(v)with_foo(|foo| ..)with_mut_foo(..)MutexRefCellIpAddrcnt#[expect(missing_docs, reason = "...")]/////!///////#[doc(hidden)]main()ctor.into().parse()::<_>assert!foranysumformat!write!+format!str::replace| Topic | Common advice | |
|---|---|---|
| Import granularity | | |
| No | |
| Loops vs combinators | Prefer iterator chains over | |
| Use them in constructors and builders everywhere | Library-only, and only when ergonomics are paramount (G#accepting-into) |
| Shared state | | Unique ownership first; |
| Traits | Design trait hierarchies; add extension traits to any type, such as | A trait only for generics, |
| Error types | | |
| Enable | Allow |
| | |