Google发布了内部Rust编程语言风格指南,用于规范在Google3、Android、ChromeOS、Fuchsia和Chrome等产品中的Rust代码使用标准1。该指南涵盖代码格式、API设计、内存管理和错误处理等多个方面1,目标是提高代码可读性、便于大规模代码变更,并与Rust生态保持一致1。
指南在代码编写层面设定了具体规范:最大行宽为100列,十六进制字面量使用小写字母1。内存管理方面,指南推荐使用所有权和借用机制,优先于Rc和Arc1。字符串操作建议采用format!()和write!()函数进行连接和替换1。在Chrome产品中禁止使用async/.await,优先选择序列而非线程处理1,同时禁止使用panic::catch_unwind(除特定异常情况外)1。
API设计规范方面,公开暴露的枚举和公开字段的结构体应分别标注#[non_exhaustive]属性,以便适应未来的代码演进1。库代码的API设计规则为强制性要求,而应用代码则为强烈建议1。
Google has published a comprehensive style guide for the Rust programming language to standardize code practices across its major products, including Chrome, Android, ChromeOS, Fuchsia, and google3 1. The guide establishes conventions covering not only code formatting but also API design, memory management, and error handling to maintain consistency and quality across Google's internal Rust codebases 1.
The style guide sets a maximum line width of 100 columns and mandates lowercase letters for hexadecimal literals 1. For memory management, the guide recommends prioritizing ownership and borrowing mechanisms over alternatives like Rc and Arc 1. For API design, the rules are enforced as mandatory requirements for library code while serving as strong recommendations for application code 1. Chrome development explicitly prohibits the use of async/.await, favoring sequential execution over threading, and bans panic::catch_unwind except in specific exceptional cases 1.
The guide provides specific guidance on string operations, recommending format!() and write!() for string concatenation and replacement 1. For public APIs, enums and structs with public fields must be annotated with the #[non_exhaustive] attribute to accommodate future evolution without breaking compatibility 1. Overall, the style guide aims to improve code readability, facilitate large-scale code changes, and maintain alignment with the broader Rust ecosystem 1.
评论
还没有评论,欢迎留下第一条。