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> has a few special features that Rust doesn't currently allow for user
- The dereference operator for
Box<T>produces a place which can be moved from. This means that the
*operator and the destructor of
Box<T>are built-in to the language.
- Methods can take
Box<Self>as a receiver.
- A trait may be implemented for
Box<T>in the same crate as
T, which the orphan rules prevent for other generic types.
std::cell::UnsafeCell<T> is used for interior mutability. It ensures that
the compiler doesn't perform optimisations that are incorrect for such types.
It also ensures that
static items which have a type with interior
mutability aren't placed in memory marked as read only.
Copy trait changes the semantics of a type implementing it. Values
whose type implements
Copy are copied rather than moved upon assignment.
Copy can only be implemented for types which do not implement
Drop, and whose fields are all
For enums, this means all fields of all variants have to be
For unions, this means all variants have to be
Copy is implemented by the compiler for
- Tuples of
- Function pointers
- Function items
- Closures that capture no values or that only capture values of
Clone trait is a supertrait of
Copy, so it also needs compiler
generated implementations. It is implemented by the compiler for the following
- Types with a built-in
Copyimplementation (see above)
- Tuples of
Send trait indicates that a value of this type is safe to send from one
thread to another.
If no explicit implementation or negative implementation is written out for an auto trait for a given type, then the compiler implements it automatically according to the following rules:
[T; n], and
[T]implement the trait if
- Function item types and function pointers automatically implement the trait.
- Structs, enums, unions, and tuples implement the trait if all of their fields do.
- Closures implement the trait if the types of all of their captures do. A
closure that captures a
Tby shared reference and a
Uby value implements any auto traits that both
For generic types (counting the built-in types above as generic over
T), if a
generic implementation is available, then the compiler does not automatically
implement it for types that could use the implementation except that they do not
meet the requisite trait bounds. For instance, the standard library implements
Send for all
Sync; this means that the compiler will not
Send but not
Auto traits can also have negative implementations, shown as
impl !AutoTrait for T in the standard library documentation, that override the automatic
implementations. For example
*mut T has a negative implementation of
*mut T is not
Send, even if
T is. There is currently no stable way
to specify additional negative implementations; they exist only in the standard
Auto traits may be added as an additional bound to any trait object, even
though normally only one trait is allowed. For instance,
Box<dyn Debug + Send + UnwindSafe> is a valid type.
Sized trait indicates that the size of this type is known at compile-time; that is, it's not a dynamically sized type.
Type parameters are
Sized by default, as are associated types.
Sized is always implemented automatically by the compiler, not by implementation items.
Sized bounds may be relaxed by using the special