Naming conventions¶
Level: 201 · working knowledge
One line: In Rust a conversion method's prefix is a promise about cost and ownership — as_ is free and borrows, to_ does work, into_ consumes self — and the rest of the naming rules (getters without get_, iter/iter_mut/into_iter, new/with_…/from_…) let a caller guess a method before looking it up.
Stub — an outline, not a lesson. There is no runnable example behind this page yet, so nothing on it has been through the check that backs every other claim in this library. The bullets below are the questions the finished page has to answer.
What it has to cover¶
- The C-CONV table from the Rust API Guidelines:
as_— free, borrowed to borrowed;to_— expensive, borrowed to borrowed or owned, or owned to owned forCopytypes;into_— variable cost, owned to owned — checked againststr::as_bytes,str::to_lowercase,f64::to_radiansandString::into_bytes - The
into_that can fail:BufWriter::into_innerflushes, so it returnsResult<W, IntoInnerError<BufWriter<W>>>— cost hidden behind a prefix that usually means "free" clippy::wrong_self_convention(style, warn by default): which prefix it pairs with whichselfreceiver, and what it says aboutfn to_x(self)on a non-Copytype- Getters:
fn first(&self) -> &First, notget_first(C-GETTER);getkept for the one obvious thing (Cell::get) and for lookups with an argument that may fail (slice::getreturningOption), with_uncheckedvariants for theunsafeversions - Iterators:
iter,iter_mutandinto_iterreturningIter,IterMutandIntoIter(C-ITER, C-ITER-TY), andclippy::iter_without_into_iter(pedantic) for theIntoIterator for &Collectionimpl aforloop needs - Constructors:
new,with_capacity-stylewith_…,from_…for conversions (C-CTOR), andclippy::new_without_default(style) asking forDefaultbeside apub fn new() -> Self - Casing from RFC 430:
UpperCamelCasetypes with acronyms as one word (Uuid, notUUID),snake_casefunctions and modules,SCREAMING_SNAKE_CASEconstants — enforced by rustc'snon_camel_case_types,non_snake_caseandnon_upper_case_globals, all warn by default - Word order (C-WORD-ORDER): the Guidelines list
ParseIntError,ParseFloatErrorand five more in verb-object-error order and recommendParseAddrErroroverAddrParseError— yet std shipsstd::net::AddrParseError. What does the guideline then ask for?
The trap it exists for¶
to_ on a method that returns a view, or as_ on one that allocates. The caller trusts the prefix, calls it in a loop, and the cost the name hid becomes the top line of the profile. The prefix is part of the signature a reader sees; the body is not.
Where this sits¶
ToOwned is the trait behind to_owned(), and iter, iter_mut, into_iter covers those three methods from the caller's side. str::as_str and RFC 1054 are two naming decisions std made and then revisited. This page is the rule a library author follows so callers can predict names.
See also¶
ToOwned:Clonefor types whose owned twin is a different type —to_in its most-used formiter,iter_mut,into_iter— the three names from the caller's side- The
Defaulttrait — the implnew_without_defaultasks for str::as_str: the method that was stabilized and taken back — anas_that had nothing to do- RFC 1054 — the method that renamed itself to promise less — a name changed because it promised too much
- Rust API Guidelines — Naming ↗ — C-CASE through C-WORD-ORDER
- RFC 430 — naming conventions ↗ — the casing rules
If you are coming from another language¶
- Python. PEP 8 gives the same casing split:
CapWordsclasses,snake_casefunctions,UPPER_CASEconstants. Python names carry no cost convention, soas_/to_/into_is new. A@propertymakes a getter look like a field; Rust has no properties, so the getter is a method named after the field. - Java. JavaBeans'
getName()andisActive()are the convention Rust drops. The wordsasandtoexist in Java too, andArrays.asListdoes return a view backed by the array whiletoArraycopies — the same distinction, without a style guide promising it. - Go. Effective Go also drops
Getfrom getters —Owner(), notGetOwner()— and names constructorsNewThing. The cost-and-ownership prefixes have no Go counterpart, because Go has no ownership to describe.
Po polsku¶
Przedrostek metody konwertującej to w Ruście obietnica: as_ jest darmowe i pożycza, to_ wykonuje pracę (często alokuje), into_ konsumuje self. Gettery nie mają przedrostka get_, iteratory nazywają się iter, iter_mut i into_iter, a konstruktory new, with_… i from_…. Pułapka: as_ na metodzie, która alokuje, albo to_ na tanim widoku — wywołujący ufa nazwie i płaci koszt, którego się nie spodziewał.
Szukaj po polsku: konwencje nazewnicze w Ruście · nazwy metod konwertujących · rust as_ to_ into_ naming · rust api guidelines naming · rust getter naming convention