Naming Conventions
This lesson discusses naming conventions in Rust!
snake_case
- All letters lower_case
- underscore separated
SCREAMING_SNAKE_CASE
- All letters uppercase
- underscore separated
CamelCase
- each word in the middle of the phrase begins with a capital letter
-
Crate - snake_case (but prefer single word)
-
Module - snake_case
mod module_name {
// module body
}
- Trait - CamelCase
trait TraitName {
// trait body
}
- Enum variants - CamelCase
enum EnumName {
Variant1
// enum body
}
- Functions- snake_case
fn fn_name() {
// function body
}
-
Methods-snake_case
-
Variables
- Local variables - snake_case
let my_var = 1
- Static variables - SCREAMING_SNAKE_CASE
static MY_STATIC_VAR = 1
- Constant variables - SCREAMING_SNAKE_CASE
const MY_CONST_VAR = 1
-
Type parameter - CamelCase, usually single uppercase letter:
T
-
Lifetimes short, lowercase: 'a
-
When a name is forbidden because it is a reserved word (e.g., crate), use a trailing underscore to make the name legal (e.g., crate_), or use raw identifiers if possible.