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...
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...
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...
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...
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...
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...
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...
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...
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...
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...
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...
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...
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...
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...
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...
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...
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...
Unsafety Unsafe operations are those that can potentially violate the memory-safety guarantees of Rust's static semantics. The following language level features cannot be used in the safe subset of Rust: • Dereferencing a raw pointer. • Rea...
The unsafe keyword The unsafe keyword is used to create or discharge the obligation to prove something safe. Specifically: • It is used to mark code that defines extra safety conditions that must be upheld elsewhere. • This includes unsafe...
Behavior considered undefined Rust code is incorrect if it exhibits any of the behaviors in the following list. This includes code within unsafe blocks and unsafe functions. unsafe only means that avoiding undefined behavior is on the progr...