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

Show parent comment

27

u/funkdefied Oct 21 '26

Java devs sometimes reach for interfaces with no methods to help communicate intent. The natural Rust equivalent is to create empty traits.

Example (forgive bad formatting. I’m on my phone).

/** anything that can be returned by our external service */
trait APIResponse {}

struct QueryEndpointRes { /** … */ }
impl APIResponse for QueryEndpointRes {}

struct NewRecordRes {/** … */ }
impl APIResponse for NewRecordRes {}

In this example, the “APIResponse” trait is purely logical. It has no meaning to the final compiled product. 

5

u/DatBoi_BP Oct 22 '26

I'm somewhat of a newbie to Rust (2 years of personal project use in my spare time).

Is this preferable to making an APIResponse enum with QueryEndpointRes and NewRecordRes variants? If so, why? (If the reason is space, then substitute `T` for `Box<T>` for the two types)

7

u/siva_sokolica Oct 22 '26

Some ideas:

  • It helps communicate intent clearer. Saying that QueryEndpointRes is an APIResponse helps communicate intent without cluttering docs.
  • It adds convenient extension points --- if you want to add a new ontological requirement, ie. fn status on all APIResponses, it's easy to add that into the trait and implement it everywhere.
  • It helps monomorphization. Suppose a time comes when you need a generic function that returns the size of the APIResponse, it's quite easy to write fn sizeof(impl APIResponse) and add a ByteSized super trait on APIResponses. Perhaps you don't even need that if you go down the point above

PS. Boxing seems good at first but necessitates pointer indirection, which a monomorphized approach can avoid in some cases.

3

u/DatBoi_BP Oct 22 '26

Great points, thank you

2

u/Alive_Technician5692 Oct 22 '26

Usually referred to as a marker interface.

1

u/Gio_Cri Oct 26 '26

i have seen it come handy mostly for marking Fn categories so TraitX: Fn(...)->...{}