Rust has a lot of terminology which you may find hard to understand or remember.
While explanatory blog posts are useful, it's also nice to have a glossary of
Rust terms which you can reference whenever you need to.
This page is intended to be updated and grow over time, as new definitions are
written or new terms become more widespread. It will also, where possible, link
to posts elsewhere on the site which describe these terms in greater depth. It
does not link to any article which uses the terms, only to ones which explain
the terms in a more complete way.
Table of Contents
- Associated Function
- Functions written in an
impl block for a type which do not take a
self parameter. These may be called as
TypeName::function_name and are commonly used for constructors or to avoid deref collision.
- The assignment of a value to a variable name. Bindings are one of two core features of patterns, alongside matching. In their simplest form they may appear as
let a = 5, where
a is bound to the value
5, or in a function like
some_function(32) where the formal parameter in the function body is bound to the actual parameter value
32. They may also appear in more complex patterns involving destructuring and binding of some internal values within the structure.
- One of two fundamental possessive states for variables in Rust, the other being ownership. Borrows are guaranteed not to exceed the lifetime of the borrowed data, to never be null, and to be either mutable exclusive-or aliasing. Rust enforces these guarantees using the borrow checker, and encodes the necessary invariants to prove these properties using lifetimes and lifetime bounds.
- Borrow, Disjoint
- Two borrows are said to be disjoint if they are of two different pieces of underlying data. In the case of two borrows from the same structure, the Rust compiler can tell if two separate fields in a
struct are borrowed and know that those borrows are disjoint.
- A mechanism for conversion of a type to another type during dereferencing, implemented by the
DerefMut traits on the initial type.
- Deref, Coercion
- Automatic conversion of a type to the type is derefs to, possibly recursively. This is performed during method calls to enable calling methods on the inner type when working with a smart pointer.
- Deref, Collision
- When an outer type which implements
DerefMut has a method which matches the name of a method on the inner type. In this case, deref coercion will fail to call the method on the inner type which may be surprising. Due to this possibility, smart pointer types often implement their functionality as associated functions.
- A function associated with a type which includes a receiver (a
self parameter), and which may be called using "dot notation" (
my_var.some_method(), as a shorthand for
- Monomorphization (also: Monomorphize)
- The process of generating concrete copies of generic functions. During monomorphization, the compiler identifies all the concrete combinations of types each generic function or type is instantiated with, and generates a copy for each unique combination, replacing the relevant calls or constructions with the non-generic version. This enables Rust generics to avoid having an impact on performance, as they compile down to normal function calls.
- A copy of data from one binding to another which invalidates the previous owner. Moves are always guaranteed to be bitwise copies of the data contained in the type, equivalent to a
memcpy of the appropriate size. Invalidation of the prior owner ensures Rust's safety guarantees can be upheld. Moves are implicit during binding. A type which implements the
Copy trait does not invalidate the prior owner, as the trait ensures that this duplication does not result in the creation of dangling resources.
- Move, Partial
- Types with fields may have those fields moved out without the overall value being moved to a new owner, so long as the prior owner of the moved field does not attempt to access it after the move.
- A mechanism for matching and binding against the structure of types. Patterns appear in variable assignment, function signatures, and any matching constructs (including
while let, and the
- Pattern, Refutable
- A pattern is refutable if it is possible for the pattern to fail to match a valid value of the relevant type.
while let, and the
matches! macro can use refutable patterns.
match requires that the overall block covers all possible values of the type (is "total") while the others conditionally execute or return a value dependent on whether matching succeeds.
- Pattern, Irrefutable
- A pattern is irrefutable if it will match any possible value of the relevant type. Patterns in assignment and function parameters must be irrefutable, as Rust doesn't permit the definition of partial functions.
- The name of the
self parameter in a method. Often written with the
&mut self shorthands, but may alternatively be written
self: Self where the
Self type may be a number of different types which deref to
- Smart Pointer
- A type of pointer to a type which 1) provides some behavior beyond a raw pointer, and 2) is designed to be used like the wrapped type as transparently as possible, through the
Deref trait and deref coercion.
- Type Ascription
- The name for explicit specification of a value's type, written in the form
value: type. An example would be
let a: i32 = 4;, which specifies the type of
a to be
i32 through type ascription.