Inline assembly Support for inline assembly is provided via the asm!, naked_asm!, and global_asm! macros. It can be used to embed handwritten assembly in the assembly output generated by the compiler. Support for inline assembly is stable o...
Linkage [!NOTE] This section is described more in terms of the compiler than of the language. The compiler supports various methods to link crates together both statically and dynamically. This section will explore the various methods to li...
Panic Rust provides a mechanism to prevent a function from returning normally, and instead "panic," which is a response to an error condition that is typically not expected to be recoverable within the context in which the error is encounte...
Variables A variable is a component of a stack frame, either a named function parameter, an anonymous temporary, or a named local variable. A local variable (or stack-local allocation) holds a value directly, allocated within the stack's me...
Memory allocation and lifetime The items of a program are those functions, modules, and types that have their value calculated at compile-time and stored uniquely in the memory image of the rust process. Items are neither dynamically alloca...
Memory model [!WARNING] The memory model of Rust is incomplete and not fully decided. Bytes The most basic unit of memory in Rust is a byte. [!NOTE] While bytes are typically lowered to hardware bytes, Rust uses an "abstract" notion of byte...
Visibility and privacy Visibility -> `pub` | `pub` `(` `crate` `)` | `pub` `(` `self` `)` | `pub` `(` `super` `)` | `pub` `(` `in` SimplePath `)` These two terms are often used interchangeably, and what they are attempting to convey is the...
Name resolution Name resolution is the process of tying paths and other identifiers to the declarations of those entities. Names are segregated into different namespaces, allowing entities in different namespaces to share the same name with...
Paths A path is a sequence of one or more path segments separated by :: tokens. Paths are used to refer to items, values, types, macros, and attributes. Two examples of simple paths consisting of only identifier segments: x; x::y::z; Types...
Preludes A prelude is a collection of names that are automatically brought into scope of every module in a crate. These prelude names are not part of the module itself: they are implicitly queried during name resolution. For example, even t...
Scopes A scope is the region of source text where a named entity may be referenced with that name. The following sections provide details on the scoping rules and behavior, which depend on the kind of entity and where it is declared. The pr...
Namespaces A namespace is a logical grouping of declared names. Names are segregated into separate namespaces based on the kind of entity the name refers to. Namespaces allow the occurrence of a name in one namespace to not conflict with th...
Names An entity is a language construct that can be referred to in some way within the source program, usually via a path. Entities include types, items, generic parameters, variable bindings, loop labels, lifetimes, fields, attributes, and...
Special types and traits Certain types and traits that exist in the standard library are known to the Rust compiler. This chapter documents the special features of these types and traits. Box<T> [Box<T>] has a few special features that Rust...
Lifetime elision Rust has rules that allow lifetimes to be elided in various places where the compiler can infer a sensible default choice. Lifetime elision in functions In order to make common patterns more ergonomic, lifetime arguments ca...
Destructors When an initialized variable or temporary goes out of scope, its destructor is run or it is dropped. Assignment also runs the destructor of its left-hand operand, if it's initialized. If a variable has been partially initialized...
Divergence A diverging expression is an expression that never completes normal execution. fn diverges() -> ! { panic!("This function never returns!"); } fn example() { let x: i32 = diverges(); // This line never completes. println!("This is...
Type coercions Type coercions are implicit operations that change the type of a value. They happen automatically at specific locations and are highly restricted in what types actually coerce. Any conversions allowed by coercion can also be...
Trait and lifetime bounds Bounds -> Bound ( `+` Bound )* `+`? Bound -> Lifetime | TraitBound | UseBound TraitBound -> ( `?` | ForLifetimes )? TypePath | `(` ( `?` | ForLifetimes )? TypePath `)` LifetimeBounds -> ( Lifetime `+` )* Lifetime?...
Subtyping and variance Subtyping is implicit and can occur at any stage in type checking or inference. Subtyping is restricted to two cases: variance with respect to lifetimes and between types with higher ranked lifetimes. If we were to er...