hn.today

Google's Rust Style Guide

google.github.io157 points98 comments
Screenshot of Google's Rust Style Guide

Google's Rust Style Guide prescribes consistent, refactor-friendly Rust usage across large Google codebases, prioritizing readability, maintainability, and automation for Large-Scale Changes. It treats formatting as only one part of "style," adding API and implementation rules. Public Rust conventions are followed where possible and rustfmt is the formatting authority. API-design rules are binding for library code and strongly recommended for application code. The guide emphasizes consistency with the wider Rust ecosystem but deliberately chooses patterns that ease tooling, large-scale refactors, and build-performance concerns in monorepo contexts.

The guide gives concrete rules: prefer small crates (often single-file) and a flat file layout in Chromium, name leaf modules after their primary type, keep implementations out of non-leaf modules, and avoid glob imports except in narrow cases. It prescribes import, ownership, and API patterns (prefer borrows over cloning, avoid Rc/Arc, accept flexible references and Into, prefer enums over bool, pair From impls with constructors, and implement standard traits via derive where possible). It covers traits, naming, concurrency, unsafe and unstable features, documentation, getters/setters, and implementation practices (prefer pattern matching, expect over unwrap, add context to errors, and avoid unnecessary iterator combinators). Exceptions and platform-specific notes (Chrome, Fuchsia, third_party Cargo layouts) are called out.

Read on google.github.io98 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.