The casing table
| Identifier | Convention | Example |
|---|---|---|
| Types, traits, enum variants | UpperCamelCase | HashMap, Iterator |
| Type parameters | UpperCamelCase, usually one letter | T |
| Modules | snake_case | btree_map |
| Functions, methods, local variables | snake_case | to_lowercase() |
| Macros | snake_case | vec! |
| Statics and constants | SCREAMING_SNAKE_CASE | PI_2 |
| Lifetimes | short lowercase | 'a |
The guidelines mark crate and feature names as unclear, so there's no official rule for those. The Rust compiler also warns by default when a name breaks these case rules, which is why Rust code is unusually consistent.
Acronyms are one word
In UpperCamelCase, an acronym or compound counts as a single word: Uuid, not UUID; Usize, not USize; Stdin, not StdIn. In snake_case it's simply lowercase: is_xid_start. A single-letter word may only come last in snake_case or SCREAMING_SNAKE_CASE, as in btree_map and PI_2.
Conversion prefixes: as_, to_, into_
| Prefix | Cost | Example |
|---|---|---|
as_ | free; borrowed to borrowed | str::as_bytes() |
to_ | expensive; may allocate | str::to_lowercase() |
into_ | variable; takes ownership | String::into_bytes() |
A wrapper type exposes its inner value with into_inner().
Getters drop the get_ prefix
A getter is named after the field: first(), with first_mut() for the mutable version. Use get only when there's one obvious thing to get, as in Cell::get.
Iterator methods
A collection offers iter() for shared references, iter_mut() for mutable references and into_iter() for owned values, and the returned types are named to match, such as IntoIter.
Converting names with Transform Case
Our snake_case, camelCase, PascalCase and CONSTANT_CASE converters treat an acronym as one word, so HTTP server error becomes HttpServerError and http_server_error. Rust also treats acronyms as one word, so the output matches the API Guidelines directly: Uuid, HttpServerError, http_server_error.
Naming conventions in other languages: Python, Java, C#, Go (Golang), Kotlin, Swift, Dart, PHP. All side by side: 20 languages compared.
Sources
- Rust API Guidelines: Naming, The Rust Project
Questions
Is it UUID or Uuid in Rust?Uuid. The Rust API Guidelines treat acronyms as one word in UpperCamelCase.
Should Rust getters start with get_?
Generally no. Name the getter after the field, such as first(); use get only when there's one obvious thing to retrieve.
What case are Rust constants?
SCREAMING_SNAKE_CASE, for both const and static items.