Google's Rust Style Guide
Google has created a style guide for the Rust programming language that outlines guidelines for usage across various codebases, including google3, Android, ChromeOS, Fuchsia, and Chrome. The purpose of this guide is to maintain consistency in Rust code and ensure quality, as Rust is used in many places at Google. The guide aims to establish a standard of quality that will help maintain the code for long periods while reducing the need for large-scale changes (LSCs) in the future.
The guide emphasizes the importance of consistency and readability, as well as encouraging good API design. It aligns with the official public Rust style rules, with some deviations to better accommodate the needs of large-scale changes at Google. For library code, the style guide rules regarding API design are binding, while for application code, they are a strong recommendation. The guide suggests avoiding premature generalization and focusing on immediate needs, rather than broad reusability.
In Chromium, the guide recommends keeping crates small, ideally contained within a single file. Large crates are allowed when necessary, such as when dealing with complex trait impl coherence rules or when building large foundational libraries. The layout of rust_library (Rust crate) source files should reside in a single directory, without using subdirectories to organize files. Exceptions to this rule exist when third-party code is built with Cargo.
When naming modules, the guide suggests naming leaf modules after the primary user-defined item (e.g., mod foo and foo.rs). Non-leaf modules should only contain module declarations and re-exports. This approach makes it easy to identify the content or purpose of a source file at a glance. The guide allows certain sub-modules, such as mod test and cross-language binding modules like #[cxx::bridge].
Written by urgent.news from Lobsters's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.
This story
This is one outlet's version. Read the fullest account.
- Google's Rust Style Guide google.github.io