Google has published a formal style guide for the Rust programming language to ensure consistency across its diverse ecosystem of projects, including Android, ChromeOS, and Fuchsia. While many Rust formatting conventions are handled automatically by tools like `rustfmt`, this guide extends beyond aesthetics to encompass API design, code organization, and implementation patterns. The primary goal is to prioritize long-term maintainability and readability, ensuring that code remains accessible to a wide range of engineers over its lifecycle.
One of the guide's most distinct features is its focus on 'Large-Scale Changes' (LSCs). Because Google manages massive codebases, the guide encourages patterns that are 'refactor-friendly'—meaning they are easily manipulated by automated tooling. For example, the guide suggests a 'flat file layout' for certain library types in Chromium, where source files reside in a single directory rather than a nested structure. This approach is designed to improve the performance of parallel compilation and build caching, even if it deviates from the conventional Cargo directory structure typically found in open-source projects.
The guide also provides specific instructions on ownership and API design, such as preferring borrows over deep copies and using `enum` types over `bool` in public APIs. It distinguishes between 'library code,' which must adhere strictly to API design rules for broad reusability, and 'application code,' where the focus shifts toward simplicity and directness. Additionally, the guide addresses technical nuances like the use of `#[non_exhaustive]` on public enums to facilitate future updates without breaking downstream consumers, and the preference for pattern matching over 'check-then-unwrap' logic to improve code safety and clarity.
While much of the guide aligns with the broader Rust ecosystem, its specific requirements for module organization and naming conventions are tailored to Google's internal infrastructure. By codifying these rules, Google aims to reduce 'bikeshedding'—minor disagreements over style—and provide a clear standard of quality that helps engineers focus on solving complex problems rather than debating formatting choices during code reviews. The guide serves as a foundational document for ensuring that Rust, as it scales within Google's infrastructure, remains predictable and maintainable for all contributors.
