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 nameParametersNotesExamples
newno self, usually ≥ 11Constructor, also cf. DefaultBox::new, std::net::Ipv4Addr::new
with_...no self, ≥ 1Alternative constructorsVec::with_capacity, regex::Regex::with_size_limit
from_...1cf. conversion traitsString::from_utf8_lossy
as_...&selfFree conversion, gives a view into datastr::as_bytes, uuid::Uuid::as_bytes
to_...&selfExpensive conversionstr::to_string, std::path::Path::to_str
into_...self (consumes)Potentially expensive conversion, cf. conversion traitsstd::fs::File::into_raw_fd
is_...&self (or none)Should probably return a boolslice::is_empty, Result::is_ok, std::path::Path::is_file
has_...&self (or none)Should probably return a boolregex_syntax::Expr::has_bytes

References