Rust Naming Conventions
- RFC 199: use
mut, move, or ref as suffixes to differentiate methods based on the mutability of their parameters.
- RFC 344 :
- how to refer to types in method names (e.g.,
&mut [T] becomes mut_slice, and *mut T becomes mut_ptr)
- RFC 445: add an
Ext suffix to extension traits
Method Naming Conventions
| Method name | Parameters | Notes | Examples |
|---|
new | no self, usually ≥ 11 | Constructor, also cf. Default | Box::new, std::net::Ipv4Addr::new |
with_... | no self, ≥ 1 | Alternative constructors | Vec::with_capacity, regex::Regex::with_size_limit |
from_... | 1 | cf. conversion traits | String::from_utf8_lossy |
as_... | &self | Free conversion, gives a view into data | str::as_bytes, uuid::Uuid::as_bytes |
to_... | &self | Expensive conversion | str::to_string, std::path::Path::to_str |
into_... | self (consumes) | Potentially expensive conversion, cf. conversion traits | std::fs::File::into_raw_fd |
is_... | &self (or none) | Should probably return a bool | slice::is_empty, Result::is_ok, std::path::Path::is_file |
has_... | &self (or none) | Should probably return a bool | regex_syntax::Expr::has_bytes |
References