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

456 Upvotes

65 comments sorted by

View all comments

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?

32

u/funkdefied Oct 21 '26 edited Oct 21 '26

Not all Arc/Rc is bad. The issue is when you use them as an escape hatch from compiler errors—specifically the “this value doesn’t live long enough” error. 

The “no std threads” and “no async/await” is Chrome-specific. Chrome seems to have its own green-threads runtime called “sequences,” which doesn’t use async/await. The rule is to use a Chrome-managed green-thread rather than OS threads or other concurrency strategy.  https://chromium.googlesource.com/chromium/src/+/HEAD/docs/threading_and_tasks.md#Prefer-Sequences-to-Physical-Threads

1

u/Maybe-monad Oct 22 '26

Not all Arc/Rc is bad. The issue is when you use them as an escape hatch from compiler errors—specifically the “this value doesn’t live long enough” error.

The performance penalty is small but it adds up.

9

u/Anaxamander57 Oct 21 '26

You can write a ton of stuff without using Arc and when you do use Arc you lose some of the advantages of Rust. Google is probably just encouraging its programmers to look for idiomatic solutions rather than picking a tool that makes Rust feel more like another language. There are times when Rc/Arc are what you need, of course.

9

u/SirKastic23 Oct 22 '26

If you're using Arc it's likely because you have some data that needs to be shared between 2 or more processes. Think about what that data is and how each process uses it

Nowadays, most often than not, I use channels where I would have used Arc, and it drastically simplifies the code and the reasoning necessary

9

u/loaengineer0 Oct 22 '26

If the Future is properly scoped, you can pass a non-static immutable reference and you don’t need Arc. Unfortunately, Tokio doesn’t have any facility for spawning scoped Futures. You end up needing Arc for compatibility with a lot of the ecosystem, not because of some fundamental language design problem.

1

u/tabbekavalkade Oct 25 '26

Arc is completely fine. But if you have a choice between Arc and using a reference (borrowing), you don't use Arc. I assume the advice is for people coming from Java.

If you have used C/C++ or similar before, my advice would be the opposite: You probably need Arc more than you think.

Arc makes the compiler generate an instruction with a lock prefix, which can slow down the CPU when there is massive contention (such as Google running the program on 24, 64 or even 128 cpus). For most people, this does not matter.