1.2 Basic conventions of the coding specification

Programming specifications are in no way written to increase the burden on developers and are intended to help developers write high-quality Rust code.

To accomplish this, the specification terms are divided into two categories: Principles and Rules.

  • A principle is a general direction that guides programming development, or refers to a class of situations. There are also a few principles that are detectable by the Rust compiler, but because the compiler diagnostic information is confusing, principles are added to help developers avoid such situations.

  • Rules, as opposed to principles, are more specific and contain positive and negative examples to further illustrate them. Some rules also add exceptions. The rules are basically detectable by lint.

Rule content Relationship with rustfmt and clippy

The specification is divided into two main parts: code style and code practice.

Code style

Code naming, formatting and comments are included in the code style.

  • The naming part, mainly by clippy lint to check, some naming rules clippy lint does not provide detection, then need to custom lint to support.
  • The format part, mainly using rustfmt to automatically modify, the rules in the coding specification describes most of the configuration items of rustfmt by category, in order to facilitate developers to make reference and develop their own configuration items. Configuration templates are also provided in the coding specification.
  • The comments section, which includes general comments and documentation comments, rule entries are regulated through a collaboration between rustfmt and clippy.

Code Practices

The code practices are categorized by Rust language features, and each language feature is summarized as much as possible for everyday coding best practices, extracted into a list of principles and rules for developers to refer to. Most of the rules are recommendations, and the rules that are required are basically security-related.

Clippy lint involves a lot of skillful lint, so it is not put into the specification.

The rules mainly focus on generic scenarios, code readability, maintainability, security, performance of the four considerations, it only covers a small part (less than 1/5) clippy lint. There are also rules that are not available in the clippy lint and require custom lint.

The focus of the code practice content is on the Unsafe Rust coding specification, which has more coding principles than rules, and which is rarely detected by Clippy lint. There are more rules that require classes.

We hope that this section will help developers avoid some common pitfalls in writing Rust code.

Coding specification content conventions

Identified by the number before the title.

  • Identified as P for Principle. Numbered as P.Element.Number.
  • Identified as G for Rule (Guideline). The numbering is G.Element.Number.
  • When there are subdirectories. Number is P.Element.SubElement.Number or G.Element.SubElement.Number.

Number is incremented from 01. Where Element is the three-letter abbreviation for the key element (corresponding to the secondary catalog in this specification) in the domain knowledge. (Terminology reference: SEI CERT C Coding Standard)

ElementExplanationElementExplanation
NAM命名 (Naming)CMT注释 (Comment)
FMT格式 (Format)TYP数据类型 (Data Type)
CNS常量 (Const)VAR变量 (Variables)
EXP表达式 (Expression)CTF控制流程 (Control Flow)
REF引用 (Reference)PTR指针 (Pointer)
STR字符串 (String)INT整数 (Integer)
MOD模块 (Module)CAR包管理 (Cargo)
MEM内存 (Memory)FUD函数设计 (Function Design)
MAC宏 (Macro)STV静态变量 (Static Variables)
GEN泛型 (Generic)TRA特质 (Trait)
ASY异步 (Async)UNS非安全 (Unsafe Rust)
SAS安全抽象 (Safety Abstract)FFI外部函数调用接口 ( Foreign Function Interface )
LAY内存布局 (Layout)ERR错误处理 (Error Handle)
CLT集合 (Collection)MTH多线程 (Multi Threads)
EMB嵌入式Rust (Embedded Rust)FIO输入输出 (In/Out)
SEC信息安全 (Security)SPT智能指针 (Smart Pointer)
UNT单元类型 (Unit)BOL布尔 (Bool)
CHR字符类型 (Char)FLT浮点数 (Float)
SLC切片类型 (Slice)TUP元组 (Tuple)
ARR固定长度数组类型 (Array)VEC动态长度数组 (Vector)
SCT结构体 (Struct)ENM枚举体 (Enum)
UNI联合体 (Union)BLN标准库内置(BuiltIn)
OBJTrait 对象 (Trait Object)LFT生命周期 (Lifetime)
BOXBox<T> 类型DRP析构函数 (Drop)
DCL声明宏 (Declarative)PRO过程宏 (Procedural)
LCK锁同步 (Lock)LKF无锁 (Lock Free)
OTH其他 (Ohters)

Reference code open source license description

All references to external code in this specification meet the MIT/Apache/Mozilla public licenses open source license!