Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Naming Conventions

Rust uses the following naming conventions:

ItemConvention
Cratessnake_case or kebab-case
Modulessnake_case
TypesPascalCase
TraitsPascalCase
Enum variantsPascalCase
Functionssnake_case
Methodssnake_case
General constructorsnew or with_more_details
Conversion constructorsfrom_some_other_type
Macrossnake_case!
Local variablessnake_case
StaticsSCREAMING_SNAKE_CASE
ConstantsSCREAMING_SNAKE_CASE
Type parametersconcise PascalCase, usually single uppercase letter: T
Lifetimesshort 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 ItemTransformationExample
Modulessnake_caseMyModulemy_module
StructsPascalCasemy_structMyStruct
UnionsPascalCasemy_unionMyUnion
EnumsPascalCasemy_enumMyEnum
EnumeratorsPascalCaseMY_VALUEMyValue
BitmasksPascalCasemy_bitmaskMyBitmask
Bit flagssnake_caseMY_FLAGmy_flag
TypedefsPascalCasemy_aliasMyAlias
InterfacesPascalCasemy_interfaceMyInterface
ExceptionsPascalCasemy_exceptionMyException
ConstantsSCREAMING_SNAKE_CASEmyConstMY_CONST
Struct memberssnake_caseMyFieldmy_field
Union variantsPascalCasemy_variantMyVariant
Operationssnake_caseDoSomethingdo_something
Parameterssnake_caseMyParammy_param

Suffix Stripping

Common type suffixes are stripped before case conversion to produce cleaner names:

  • _t suffix: my_type_tMyType
  • _e suffix: my_enum_eMyEnum

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,
}
}