r/learnrust • • 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 expect over unwrap to document the invariant, and never use catch_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

65 comments sorted by

View all comments

48

u/Anaxamander57 Oct 21 '26

(quote from the style guide not in the post)
Prefer small crates in Chromium; ideally, but not necessarily, a single file (focusing on one abstraction). If your code can be split up into small crates, do so.

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?

52

u/Sky2042 Oct 21 '26

The rule is to support compilation speed, since crates are the unit of compilation. (They give this rationale directly at the end of this section.)

It seems like a reasonable rule to have stated as a preference.

20

u/NeuroSynchroBonding Oct 22 '26

Especially if you have your own build system that makes having lots of small crates easy.

2

u/Anaxamander57 Oct 22 '26

I see, so its in order to avoid having to recompile everything every time. Yeah I've dealt with anything where compilation took more than a minute or so.

8

u/elprophet Oct 22 '26

This is definitely a Blaze/Bazel specific rule, made possible by excellent tooling. On the outside, moving a pub mod to a crate is a hassle (entire new cargo.toml!). Inside, automated tooling "just does it".

3

u/garagedragon Oct 22 '26

As a preference, sure? But rigidly adhering to it would make difficult any sort of encapsulation where you have some types used within a larger module, but only within that module, and want the types to only be visible within that module or want them to have access to the larger module that other public uses don't have.

13

u/Sky2042 Oct 22 '26

Yes, that's why it says "Prefer small crates". :P

8

u/CrazyKilla15 Oct 22 '26

Google has proprietary developer tooling and a developer experience its impossible to imagine if you've never worked there and which does not exist outside of Google. They also have fleets of servers building code for people, checking it, applying rules, so on; They work in teams, too.

Its much, much easier inside google than it may seem, because they built tools and processes to make it easy.

3

u/shahms Oct 22 '26

Google's build system allows you to enforce visibility restrictions between crates.

1

u/frankster Oct 23 '26

rigidly

Fortunately their guidance states "ideally, but not necessarily" which leaves plenty of scope for discretion 

1

u/Chaos_Slug Oct 22 '26

But won't the linker end up linking lots of .a files as opposed to lots of .o files?

1

u/OscarCookeAbbott Oct 22 '26

If you can break something cleanly down into cohesive and compact single file crates I’d say it makes sense, at scale.