Naming Conventions
Rust uses the following naming conventions:
| Item | Convention |
|---|---|
| Crates | snake_case or kebab-case |
| Modules | snake_case |
| Types | PascalCase |
| Traits | PascalCase |
| Enum variants | PascalCase |
| Functions | snake_case |
| Methods | snake_case |
| General constructors | new or with_more_details |
| Conversion constructors | from_some_other_type |
| Macros | snake_case! |
| Local variables | snake_case |
| Statics | SCREAMING_SNAKE_CASE |
| Constants | SCREAMING_SNAKE_CASE |
| Type parameters | concise PascalCase, usually single uppercase letter: T |
| Lifetimes | short lowercase, usually a single letter: 'a, 'de, 'src |
Most of the above naming conventions are enforced by the compiler. Deviating from the above scheme will result in a compilation warning. For that reason, the generated code should follow the above naming scheme to the extent possible.
IDL to Rust Name Transformation
IDL names are automatically transformed to match Rust conventions. The following transformations are applied:
| IDL Item | Transformation | Example |
|---|---|---|
| Modules | snake_case | MyModule → my_module |
| Structs | PascalCase | my_struct → MyStruct |
| Unions | PascalCase | my_union → MyUnion |
| Enums | PascalCase | my_enum → MyEnum |
| Enumerators | PascalCase | MY_VALUE → MyValue |
| Bitmasks | PascalCase | my_bitmask → MyBitmask |
| Bit flags | snake_case | MY_FLAG → my_flag |
| Typedefs | PascalCase | my_alias → MyAlias |
| Interfaces | PascalCase | my_interface → MyInterface |
| Exceptions | PascalCase | my_exception → MyException |
| Constants | SCREAMING_SNAKE_CASE | myConst → MY_CONST |
| Struct members | snake_case | MyField → my_field |
| Union variants | PascalCase | my_variant → MyVariant |
| Operations | snake_case | DoSomething → do_something |
| Parameters | snake_case | MyParam → my_param |
Suffix Stripping
Common type suffixes are stripped before case conversion to produce cleaner names:
_tsuffix:my_type_t→MyType_esuffix:my_enum_e→MyEnum
Enum Prefix Stripping
Enumerator names that share a common prefix with their enum type have that prefix stripped to avoid redundancy:
// IDL
enum Color {
COLOR_RED,
COLOR_GREEN,
COLOR_BLUE
};
#![allow(unused)]
fn main() {
// Rust - prefix "COLOR_" is stripped
pub enum Color {
Red,
Green,
Blue,
}
}