r/learnrust • u/Western-Obligation47 • 1d ago
Google published its internal Rust style guide
Google just published its Rust style guide, which it says is developed in consultation with Android, Chromium, Fuchsia and Chrome. Most of it is uncontroversial, but a few rules go against what most of the ecosystem does:
- Flat file layout. Crates live in a single directory with no subdirectories. If you feel the need to organise a crate into folders, Google says that's a sign the crate is too big and should be split.
- No glob imports, except for merging C++ namespaces.
- Avoid Rc and Arc. Prefer unique ownership and borrowing. Google explicitly says engineers coming from other languages reach for shared ownership too quickly.
- No async/.await in Chromium, and don't start threads through std.
- Avoid panics, prefer
expectoverunwrapto document the invariant, and never usecatch_unwind. - Library vs application code. API design rules are binding for libraries but only a strong recommendation for apps, where simplicity should win over reusability.
- Don't use traits to organise code. Traits are for generics,
impl Trait/dyn Trait, and extension traits.
The stated reason for the deviations is Large-Scale Changes. Google wants code that tooling can refactor across the entire codebase, which explains the flat layout and the anti-glob-import stance more than readability does.
Update
Forgot to attach the link https://google.github.io/styleguide/rustguide.html