Interior mutability Sometimes a type needs to be mutated while having multiple aliases. In Rust this is achieved using a pattern called interior mutability. A type has interior mutability if its internal state can be changed through a share...
Type layout The layout of a type is its size, alignment, and the relative offsets of its fields. For enums, how the discriminant is laid out and interpreted is also part of type layout. Type layout can be changed with each compilation. Inst...
Dynamically sized types Most types have a fixed size that is known at compile time and implement the trait Sized. A type with a size that is known only at run-time is called a dynamically sized type (DST) or, informally, an unsized type. Sl...
Inferred type InferredType -> `_` The inferred type asks the compiler to infer the type if possible based on the surrounding information available. [!EXAMPLE] The inferred type is often used in generic arguments: let x: Vec<_> = (0..10).col...
Type parameters Within the body of an item that has type parameter declarations, the names of its type parameters are types: fn to_vec<A: Clone>(xs: &[A]) -> Vec<A> { if xs.is_empty() { return vec![]; } let first: A = xs[0].clone(); let mut...
Impl trait ImplTraitType -> `impl` Bounds ImplTraitTypeOneBound -> `impl` TraitBound impl Trait provides ways to specify unnamed but concrete types that implement a specific trait. It can appear in two sorts of places: argument position (wh...
Trait objects TraitObjectType -> `dyn`? Bounds TraitObjectTypeOneBound -> `dyn`? TraitBound A trait object is an opaque value of another type that implements a set of traits. The set of traits is made up of a dyn compatible base trait plus...
Function pointer types BareFunctionType -> ForLifetimes? FunctionTypeQualifiers `fn` `(` FunctionParametersMaybeNamedVariadic? `)` BareFunctionReturnType? FunctionTypeQualifiers -> `unsafe`? (`extern` Abi?)? BareFunctionReturnType -> `->` T...
Pointer types All pointers are explicit first-class values. They can be moved or copied, stored into data structs, and returned from functions. References (& and &mut) ReferenceType -> `&` Lifetime? `mut`? TypeNoBounds Shared references (&)...
Closure types A closure expression produces a closure value with a unique, anonymous type that cannot be written out. A closure type is approximately equivalent to a struct which contains the captured values. For instance, the following clo...
Function item types When referred to, a function item, or the constructor of a tuple-like struct or enum variant, yields a zero-sized value of its function item type. That type explicitly identifies the function - its name, its type argumen...
Union types A union type is a nominal, heterogeneous C-like union, denoted by the name of a union item. Unions have no notion of an "active field". Instead, every union access transmutes parts of the content of the union to the type of the...
Enumerated types An enumerated type is a nominal, heterogeneous disjoint union type, denoted by the name of an enum item. [^enumtype] An enum item declares both the type and a number of variants, each of which is independently named and has...
Struct types A struct type is a heterogeneous product of other types, called the fields of the type.[^structtype] New instances of a struct can be constructed with a struct expression. The memory layout of a struct is undefined by default t...
Slice types SliceType -> `[` Type `]` A slice is a dynamically sized type representing a 'view' into a sequence of elements of type T. The slice type is written as [T]. Slice types are generally used through pointer types. For example: • &[...
Array types ArrayType -> `[` Type `;` Expression `]` An array is a fixed-size sequence of N elements of type T. The array type is written as [T; N]. The size is a constant expression that evaluates to a usize. Examples: // A stack-allocated...
Tuple types TupleType -> `(` `)` | `(` ( Type `,` )+ Type? `)` Tuple types are a family of structural types[^1] for heterogeneous lists of other types. The syntax for a tuple type is a parenthesized, comma-separated list of types. 1-ary tup...
Never type NeverType -> `!` The never type ! is a type with no values, representing the result of computations that never complete. Expressions of type ! can be coerced into any other type. The ! type can only appear in function return type...
String slice type The string slice (str) type represents a sequence of characters. let greeting1: &str = "Hello, world!"; let greeting2: &str = "你好,世界"; [!NOTE] See [the standard library docs][str] for information on the impls of the str ty...
Character type The char type represents a single Unicode scalar value (i.e., a code point that is not a surrogate). [!EXAMPLE] let c: char = 'a'; let emoji: char = '😀'; let unicode: char = '\u{1F600}'; [!NOTE] See [the standard library docs...