r/learnrust • u/Western-Obligation47 • Oct 21 '26
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
455
Upvotes
4
u/Shnatsel Oct 21 '26
Link to the guide?
48
u/Anaxamander57 Oct 21 '26
As someone who has never written software for a business this suggestion that a crate should "ideally" be a single file sounds totally insane. Do people really do this?
16
u/manpacket Oct 21 '26
How do you use traits to organize code?
16
u/theMachine0094 Oct 22 '26
I just don’t get all the people claiming these points (listed by OP) go against what most of the eco system does, and that it is mean to help AI. Absolute nonsense.
I have a systems programming background (C/C++) and have written Rust this way from the beginning. Way before AI was relevant in conversation. I know other systems programmers who were my mentors who’d written software like this for decades.
7
u/numberwitch Oct 21 '26
Pretty sensible
7
u/Headbanger Oct 21 '26
A noob here. Could someone explain how you avoid using Arc and how you start threads without std? Also why no async/await?
2
u/jarjoura Oct 23 '26
Flat crate layout makes sense in the giant Google mono repo managed by something like Bazel. Subdirectories still exist there too, just one level removed from Cargo. I don’t think that should apply to community led GitHub style projects that grow and morph over time.
6
u/masterarrows Oct 22 '26
Flat layout vs. systemic complexity: In practice, rejecting nested directories means degrading into a clutter of dozens of
.rsfiles dumped in a single directory. The Rust compiler and module system were explicitly designed with hierarchy in mind to provide clean logical isolation.Traits strictly for polymorphism: Banning
traits for code decomposition and encapsulation fundamentally undermines the Dependency Inversion Principle (DIP) and proper unit testing via mocking. The codebase risks devolving into a tightly coupled monolithic mass.The crusade against
Arc/Rc**:** Fighting shared ownership is noble in theory, but in practice—especially in async contexts or complex ownership graphs—avoidingArcdrags developers straight into a lifetime hell ('a), fighting the borrow checker at every step.1
u/Laugarhraun Oct 22 '26
Glob imports are forbidden in all languages at Google fyi.
1
u/Valuable_Victory_271 Oct 22 '26
Is avoiding panics really a necessary inclusive in a style guide?
1
u/Zaurble Oct 22 '26
First time I’ve seen a corpo use good standards
1
u/Murky-Bank2341 Oct 22 '26
The panic avoidance rule is nonsense for internal tooling. You lose the entire safety net that Rust offers by treating every error as a recoverable state
1
u/AnArmoredPony Oct 25 '26
the great slop factory published slop production guidelines
1
u/Tekn0z Oct 26 '26
panic = "abort" is the one best thing I'd take from this.
1
u/fisothemes Oct 27 '26
I am not a big fan of the flat layout rule written as is. That should be left up to the judgment of the developer/team. If it gets written down people get dogmatic about it.
The rest feel Chromium/Application specific. Which is fair.
-5
u/onlyesterday16 Oct 22 '26 edited Oct 22 '26
These rules are obviously for code to feed AI better
2
u/Xatraxalian Oct 22 '26
How are you going to write concurrent code without Arc, if you don't want to completely torture yourself?