box
import std::alloc::box; — in the prelude · source
Types
Box<T, A>
type struct Box<T, A = GlobalAlloc> where A: Allocator {
ptr: T*;
alloc: A;
static new(value: T) -> Box<T, GlobalAlloc>;
static new_in(value: T, alloc: A) -> Box<T, A>;
static try_new(value: T) -> Result<Box<T, GlobalAlloc>, AllocError>;
static try_new_in(value: T, alloc: A) -> Result<Box<T, A>, AllocError>;
static from_raw(raw: T*) -> Box<T, GlobalAlloc>;
as_ptr(&this) -> T*;
get_ref(&this) -> T*;
get_mut(mut &this) -> T*;
into_raw(mut this) -> T*;
leak(mut this) -> T*;
}
A Box owns exactly one heap value plus the allocator instance that produced it. Moves transfer ownership; the final holder's drop runs T::drop() and returns the storage.
mut b: Box<Config> = Box::<Config>::new(config);
b.port = 8080; // auto-deref through Deref<T>
Note that into_raw and leak transfer both the value and the allocation, so T::drop() does not run on those paths.
Properties
| Property | Type |
|---|---|
ptr | T* |
alloc | A |
Constructors
static new(value: T) -> Box<T, GlobalAlloc>;
static new(value: T) -> Box<T, GlobalAlloc>;Allocate heap storage via GlobalAlloc and move value into it. Panics if the allocator refuses the request; use try_new to recover from out-of-memory.
static new_in(value: T, alloc: A) -> Box<T, A>;
static new_in(value: T, alloc: A) -> Box<T, A>;Allocate with a caller-supplied allocator. Panics on allocation failure; use try_new_in for fallible construction.
static try_new(value: T) -> Result<Box<T, GlobalAlloc>, AllocError>;
static try_new(value: T) -> Result<Box<T, GlobalAlloc>, AllocError>;Fallible constructor backed by GlobalAlloc. Returns Err(AllocError) if the allocator refuses the request (out of memory, zero-sized T, etc.).
static try_new_in(value: T, alloc: A) -> Result<Box<T, A>, AllocError>;
static try_new_in(value: T, alloc: A) -> Result<Box<T, A>, AllocError>;Fallible constructor with a caller-supplied allocator. The allocator instance is stored in the resulting Box and is used to free the storage on drop.
static from_raw(raw: T*) -> Box<T, GlobalAlloc>;
static from_raw(raw: T*) -> Box<T, GlobalAlloc>;Re-wrap a pointer previously produced by into_raw (or leak). The pointer must have come from a Box<T, GlobalAlloc>; using this constructor on a pointer that was allocated by a different allocator yields a Box that will free with the wrong allocator on drop. Custom-allocator users that need this round-trip should reconstitute the allocator + pointer themselves and build Box<T, A> { ptr: ..., alloc: ... } directly.
Methods
as_ptr(&this) -> T*;
as_ptr(&this) -> T*;Raw pointer to the owned value. Valid for the lifetime of the Box; dereferencing after drop is undefined.
get_ref(&this) -> T*;
get_ref(&this) -> T*;Borrow the boxed value. Sound for the lifetime of the Box. This is the ergonomic read/write accessor to prefer over as_ptr until language-level auto-deref lands: *b.get_ref() / b.get_ref().field.
get_mut(mut &this) -> T*;
get_mut(mut &this) -> T*;Mutable borrow of the boxed value. A Box is a unique owner, so this is always sound (unlike Rc/Arc, where mutation must be gated on uniqueness).
into_raw(mut this) -> T*;
into_raw(mut this) -> T*;Give up ownership and return the raw pointer. The caller becomes responsible for freeing the memory (typically via Box::from_raw); dropping this box afterwards would double-free.
leak(mut this) -> T*;
leak(mut this) -> T*;Bridge alias for into_raw, kept so legacy callers reading the new stdlib can still spell "leak this allocation." Same behavior: give up ownership and return the raw pointer.
Trait implementations
implement<T, A> trait Drop for struct Box<T, A> where A: Allocator
implement<T> trait Clone for struct Box<T, GlobalAlloc> where T: Clone
implement<T, A> trait Deref<T> for struct Box<T, A>
Deref<T> is what makes *b, b.field, and b.method() work directly on the box. Clone is a deep copy into fresh GlobalAlloc storage.
type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct Box<T, A = GlobalAlloc> where A: Allocator {
ptr: T*;
alloc: A;
}Box<T, A>: single-owner heap allocation.
type parameter Ttype parameter Awhere A: Allocator
type struct GlobalAlloc {}Zero-sized handle to the process-wide allocator. Construct with GlobalAlloc::new(); all instances dispatch through the same underlying backend.
where T: Ord + Clone, U: HashConstrains generic parameters to types that implement the listed traits, which is what makes those traits' methods callable on T. Bounds on one parameter join with +; parameters separate with ,. A where on a single method narrows the type's parameter for that method only.
type trait Allocator {
allocate(mut &this, layout: Layout) -> Result<NonNull<u8>, AllocError>;
deallocate(mut &this, ptr: NonNull<u8>, layout: Layout) -> void;
reallocate(mut &this, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout) -> Result<NonNull<u8>, AllocError>;
}Allocator: the trait every heap strategy implements.
std::alloc::allocatorBox.ptr: T*type parameter TBox.alloc: Astatic name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
static Box::new(value: T) -> Box<T, GlobalAlloc>Allocate heap storage via GlobalAlloc and move value into it. Panics if the allocator refuses the request; use try_new to recover from out-of-memory.
(parameter) value: Ttype struct Box<T, A = GlobalAlloc> where A: Allocator {
ptr: T*;
alloc: A;
}Box<T, A>: single-owner heap allocation.
std::alloc::boxstatic Box::new_in(value: T, alloc: A) -> Box<T, A>Allocate with a caller-supplied allocator. Panics on allocation failure; use try_new_in for fallible construction.
(parameter) alloc: Astatic Box::try_new(value: T) -> Result<Box<T, GlobalAlloc>, AllocError>Fallible constructor backed by GlobalAlloc. Returns Err(AllocError) if the allocator refuses the request (out of memory, zero-sized T, etc.).
type struct AllocError {
kind: AllocErrorKind;
layout: Layout;
}static Box::try_new_in(value: T, alloc: A) -> Result<Box<T, A>, AllocError>Fallible constructor with a caller-supplied allocator. The allocator instance is stored in the resulting Box and is used to free the storage on drop.
static Box::from_raw(raw: T*) -> Box<T, GlobalAlloc>Re-wrap a pointer previously produced by into_raw (or leak). The pointer must have come from a Box<T, GlobalAlloc>; using this constructor on a pointer that was allocated by a different allocator yields a Box that will free with the wrong allocator on drop.
(parameter) raw: T*Box::as_ptr(&this) -> T*Raw pointer to the owned value. Valid for the lifetime of the Box; dereferencing after drop is undefined.
&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
Box::get_ref(&this) -> T*Borrow the boxed value. Sound for the lifetime of the Box. This is the ergonomic read/write accessor to prefer over as_ptr until language-level auto-deref lands: *b.get_ref() / b.get_ref().field.
Box::get_mut(mut &this) -> T*Mutable borrow of the boxed value. A Box is a unique owner, so this is always sound (unlike Rc/Arc, where mutation must be gated on uniqueness).
mut &this
mut &TypeAn exclusive, mutating borrow. As a receiver, mut &this lets the method modify the fields - and callers can see that from the signature alone. As a parameter type, mut &T lets the callee mutate through the reference while the caller keeps ownership.
Box::into_raw(mut this) -> T*Give up ownership and return the raw pointer. The caller becomes responsible for freeing the memory (typically via Box::from_raw); dropping this box afterwards would double-free.
mut thisA by-value receiver the body may also reassign. The receiver is moved into the method and consumed; the caller's value may not be used afterwards.
language referenceBox::leak(mut this) -> T*Bridge alias for into_raw, kept so legacy callers reading the new stdlib can still spell "leak this allocation." Same behavior: give up ownership and return the raw pointer.
mut name: Type = value;A mutable binding, reassignable after declaration - mutability is opt-in, const is the default. At module level it declares mutable global state. Before & it marks an exclusive reference or receiver instead.
mut b: Box<Config>static Box::new(value: T) -> Box<T, GlobalAlloc>Allocate heap storage via GlobalAlloc and move value into it. Panics if the allocator refuses the request; use try_new to recover from out-of-memory.
mut b: Box<Config>import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
Box::into_raw(mut this) -> T*Give up ownership and return the raw pointer. The caller becomes responsible for freeing the memory (typically via Box::from_raw); dropping this box afterwards would double-free.
Box::leak(mut this) -> T*Bridge alias for into_raw, kept so legacy callers reading the new stdlib can still spell "leak this allocation." Same behavior: give up ownership and return the raw pointer.
Box.ptr: T*Box.alloc: Astatic Box::try_new(value: T) -> Result<Box<T, GlobalAlloc>, AllocError>Fallible constructor backed by GlobalAlloc. Returns Err(AllocError) if the allocator refuses the request (out of memory, zero-sized T, etc.).
static Box::new_in(value: T, alloc: A) -> Box<T, A>Allocate with a caller-supplied allocator. Panics on allocation failure; use try_new_in for fallible construction.
static Box::try_new_in(value: T, alloc: A) -> Result<Box<T, A>, AllocError>Fallible constructor with a caller-supplied allocator. The allocator instance is stored in the resulting Box and is used to free the storage on drop.
Result::Err(E)static Box::from_raw(raw: T*) -> Box<T, GlobalAlloc>Re-wrap a pointer previously produced by into_raw (or leak). The pointer must have come from a Box<T, GlobalAlloc>; using this constructor on a pointer that was allocated by a different allocator yields a Box that will free with the wrong allocator on drop.
function name(count: i32, args...)A variadic parameter: the trailing bucket receives any number of further arguments with C's calling convention, so the function stays ABI-compatible with the printf family. Wrap the bucket in VaArgs to read typed values from it.
Box::as_ptr(&this) -> T*Raw pointer to the owned value. Valid for the lifetime of the Box; dereferencing after drop is undefined.
Box::get_ref(&this) -> T*Borrow the boxed value. Sound for the lifetime of the Box. This is the ergonomic read/write accessor to prefer over as_ptr until language-level auto-deref lands: *b.get_ref() / b.get_ref().field.
Box::get_mut(mut &this) -> T*Mutable borrow of the boxed value. A Box is a unique owner, so this is always sound (unlike Rc/Arc, where mutation must be gated on uniqueness).
type struct Rc<T, A = GlobalAlloc> where A: Allocator {
inner: RcInner<T, A>*;
}Rc<T, A>: single-threaded reference counting, with weak references.
std::alloc::rctype struct Arc<T, A = GlobalAlloc> where A: Allocator {
inner: ArcInner<T, A>*;
}Arc<T, A>: atomic reference counting, with weak references.
std::alloc::arcimplement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
type trait Name : Base {
method(&this, other: &This) -> Ret;
}Names a set of methods a type may implement. Inside the body This is the implementing type; methods may carry default bodies, and a trait may require a base trait (type trait Ord : Eq). Generic code asks for a trait with where T: Name.
implement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language referencetype parameter Twhere T: Clone
type parameter Atype trait Deref<Target> {
deref(&this) -> Target*;
}Deref<Target> exposes the value a smart pointer points at. The returned Target* is mutable (Cryo pointers are not const-qualified), so this one trait covers both shared and mutable access - there is no separate DerefMut.