Rust Reference ยท Concepts
Rust naming conventions
Reference for Rust naming: snake_case, UpperCamelCase and SCREAMING_SNAKE_CASE, getter, conversion and constructor naming (as_, to_, into_, new, with_), lints and API guidelines.
Rust has strong, mostly compiler-enforced naming conventions, so code from different authors looks alike. The compiler warns about violations (non_snake_case, non_camel_case_types, non_upper_case_globals), and the Rust API Guidelines extend the rules to method names and crate design.
The basic case rules: snake_case for functions, methods, variables, modules and crates; UpperCamelCase for types, traits, enum variants and type parameters (usually a single capital letter like T); SCREAMING_SNAKE_CASE for constants and statics; short lowercase lifetimes like 'a.
const MAX_RETRIES: u32 = 3; struct HttpRequest; fn parse_header() {}
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Casing rules
| Item | Convention | Example |
|---|---|---|
| Crates, modules, functions, methods, locals, fields | snake_case |
read_line, user_id |
| Types, traits, enum variants | UpperCamelCase |
HashMap, IntoIterator, None |
| Constants, statics | SCREAMING_SNAKE_CASE |
MAX_SIZE |
| Generic parameters | short UpperCamelCase, often T, K, V, E |
Result<T, E> |
| Lifetimes | short lowercase | 'a, 'de, 'static |
| Macros | snake_case! |
println!, vec! |
| Features (Cargo) | lowercase, usually kebab-case or snake_case |
serde-support |
| Acronyms in types | treated as one word | Uuid, HttpServer, not UUID |
Method and function naming
| Pattern | Meaning |
|---|---|
new |
the primary constructor; Default::default for no-argument construction |
with_xxx |
constructor or builder step with a setting (Vec::with_capacity) |
from_xxx |
constructor from another representation (String::from_utf8) |
xxx() / set_xxx() |
getter / setter (no get_ prefix; get is for lookups that may fail) |
xxx_mut() |
mutable variant of a getter |
is_xxx, has_xxx |
boolean queries |
as_xxx |
cheap borrowed-to-borrowed conversion |
to_xxx |
costly conversion from a borrow, usually allocating |
into_xxx |
consumes self, returns another type (often cheap) |
iter, iter_mut, into_iter |
the three iterator flavors |
try_xxx |
fallible version returning Result or Option |
xxx_unchecked |
skips a check; usually unsafe |
xxx_or, xxx_or_else, xxx_or_default |
fallbacks for Option and Result |
checked_, wrapping_, saturating_, overflowing_ |
integer arithmetic families |
Usage notes
- Leading underscore (
_unused) silences the unused-variable warning; a lone_discards the value immediately and does not bind it. - Trailing underscore or raw identifiers avoid keywords:
type_orr#type. - Conversion traits first: if a type can implement
From,AsRef,FromStrorIterator, do that instead of inventingfrom_foo/as_foomethods for the same job. - Names read as English at the call site:
file.read_to_string(&mut s)andlist.is_empty(). - Avoid module name repetition:
http::Request, nothttp::HttpRequest; the path already says it. - Acronyms and units go in the name when ambiguous:
timeout_ms,size_bytes; prefer typed values (Duration) where possible. - Tools:
cargo fmtfixes layout;cargo clippyadds more style lints; the naming lints above ship with rustc.