|
| 1 | +# Special types and traits |
| 2 | + |
| 3 | +Certain types and traits that exist in [the standard library] are known to the |
| 4 | +Rust compiler. This chapter documents the special features of these types and |
| 5 | +traits. |
| 6 | + |
| 7 | +## `Box<T>` |
| 8 | + |
| 9 | +[`Box<T>`] has a few special features that Rust doesn't currently allow for user |
| 10 | +defined types. |
| 11 | + |
| 12 | +* The [dereference operator] for `Box<T>` produces a place which can be moved |
| 13 | + from. This means that the `*` operator and the destructor of `Box<T>` are |
| 14 | + built-in to the language. |
| 15 | +* [Methods] can take `Box<Self>` as a receiver. |
| 16 | +* A trait may be implemented for `Box<T>` in the same crate as `T`, which the |
| 17 | + [orphan rules] prevent for other generic types. |
| 18 | + |
| 19 | +## `UnsafeCell<T>` |
| 20 | + |
| 21 | +[`std::cell::UnsafeCell<T>`] is used for [interior mutability]. It ensures that |
| 22 | +the compiler doesn't perform optimisations that are incorrect for such types. |
| 23 | +It also ensures that [`static` items] which have a type with interior |
| 24 | +mutability aren't placed in memory marked as read only. |
| 25 | + |
| 26 | +## `PhantomData<T>` |
| 27 | + |
| 28 | +[`std::marker::PhantomData<T>`] is a zero-sized, minimum alignment, type that |
| 29 | +is considered to own a `T` for the purposes of [variance], [drop check] and |
| 30 | +[auto traits](#auto-traits). |
| 31 | + |
| 32 | +## Operator Traits |
| 33 | + |
| 34 | +The traits in [`std::ops`] and [`std::cmp`] are used to overload [operators], |
| 35 | +[indexing expressions] and [call expressions]. |
| 36 | + |
| 37 | +## `Deref` and `DerefMut` |
| 38 | + |
| 39 | +As well as overloading the unary `*` operator, [`Deref`] and [`DerefMut`] are |
| 40 | +also used in [method resolution] and [deref coercions]. |
| 41 | + |
| 42 | +## `Drop` |
| 43 | + |
| 44 | +The [`Drop`] trait provides a [destructor], to be run whenever a value of this |
| 45 | +type is to be destroyed. |
| 46 | + |
| 47 | +## `Copy` |
| 48 | + |
| 49 | +The [`Copy`] trait changes the semantics of a type implementing it. Values |
| 50 | +whose type implements `Copy` are copied rather than moved upon assignment. |
| 51 | +`Copy` cannot be implemented for types which implement `Drop`, or which have |
| 52 | +fields that are not `Copy`. `Copy` is implemented by the compiler for |
| 53 | + |
| 54 | +* [Numeric types] |
| 55 | +* `char` and `bool` |
| 56 | +* [Tuples] of `Copy` types |
| 57 | +* [Arrays] of `Copy` types |
| 58 | +* [Shared references] |
| 59 | +* [Raw pointers] |
| 60 | +* [Function pointers] and [function item types] |
| 61 | + |
| 62 | +## `Clone` |
| 63 | + |
| 64 | +The [`Clone`] trait is a supertrait of `Copy`, so it also needs compiler |
| 65 | +generated implementations. It is implemented by the compiler for the following |
| 66 | +types: |
| 67 | + |
| 68 | +* Types with a built-in `Copy` implementation (see above) |
| 69 | +* [Tuples] of `Clone` types |
| 70 | +* [Arrays] of `Clone` types |
| 71 | + |
| 72 | +## `Send` |
| 73 | + |
| 74 | +The [`Send`] trait indicates that a value of this type is safe to send from one |
| 75 | +thread to another. |
| 76 | + |
| 77 | +## `Sync` |
| 78 | + |
| 79 | +The [`Sync`] trait indicates that a value of this type is safe to share between |
| 80 | +multiple threads. This trait must be implemented for all types used in |
| 81 | +immutable [`static` items]. |
| 82 | + |
| 83 | +## Auto traits |
| 84 | + |
| 85 | +The [`Send`], [`Sync`], [`UnwindSafe`] and [`RefUnwindSafe`] traits are _auto |
| 86 | +traits_. Auto traits have special properties. |
| 87 | + |
| 88 | +First, auto traits are automatically implemented using the following rules: |
| 89 | + |
| 90 | +* `&T`, `&mut T`, `*const T`, `*mut T`, `[T; n]` and `[T]` implement the trait |
| 91 | + if `T` does. |
| 92 | +* Function item types and function pointers automatically implement the trait. |
| 93 | +* Structs, enums, unions and tuples implement the trait if all of their fields |
| 94 | + do. |
| 95 | +* Closures implement the trait if the types of all of their captures do. A |
| 96 | + closure that captures a `T` by shared reference and a `U` by value implements |
| 97 | + any auto traits that both `&T` and `U` do. |
| 98 | + |
| 99 | +Auto traits can also have negative implementations, shown as `impl !AutoTrait |
| 100 | +for T` in the standard library documentation, that override the automatic |
| 101 | +implementations. For example `*mut T` has a negative implementation of `Send`, |
| 102 | +and so `*mut T` and `(*mut T,)` are not `Send`. Finally, auto traits may |
| 103 | +be added as a bound to any [trait object]\: `Box<Debug + Send + UnwindSafe>` is |
| 104 | +a valid type. |
| 105 | + |
| 106 | +## `Sized` |
| 107 | + |
| 108 | +The [`Sized`] trait indicates that the size of this type is known at |
| 109 | +compile-time; that is, it's not a [dynamically sized type]. [Type parameters] |
| 110 | +are `Sized` by default. `Sized` is always implemented automatically by the |
| 111 | +compiler, not by [implementation items]. |
| 112 | + |
| 113 | +[`Box<T>`]: ../std/boxed/struct.Box.html |
| 114 | +[`Clone`]: ../std/clone/trait.Clone.html |
| 115 | +[`Copy`]: ../std/marker/trait.Copy.html |
| 116 | +[`Deref`]: ../std/ops/trait.Deref.html |
| 117 | +[`DerefMut`]: ../std/ops/trait.DerefMut.html |
| 118 | +[`Drop`]: ../std/ops/trait.Drop.html |
| 119 | +[`RefUnwindSafe`]: ../std/panic/trait.RefUnwindSafe.html |
| 120 | +[`Send`]: ../std/marker/trait.Send.html |
| 121 | +[`Sized`]: ../std/marker/trait.Sized.html |
| 122 | +[`std::cell::UnsafeCell<T>`]: ../std/cell/struct.UnsafeCell.html |
| 123 | +[`std::cmp`]: ../std/cmp/index.html |
| 124 | +[`std::marker::PhantomData<T>`]: ../std/marker/struct.PhantomData.html |
| 125 | +[`std::ops`]: ../std/ops/index.html |
| 126 | +[`UnwindSafe`]: ../std/panic/trait.UnwindSafe.html |
| 127 | +[`Sync`]: ../std/marker/trait.Sync.html |
| 128 | + |
| 129 | +[Arrays]: types.html#array-and-slice-types |
| 130 | +[call expressions]: expressoins/call-expr.html |
| 131 | +[deref coercions]: type-coercions.html#coercion-types |
| 132 | +[dereference operator]: expressions/operator-expr.html#the-dereference-operator |
| 133 | +[destructor]: destructors.html |
| 134 | +[drop check]: ../nomicon/dropck.html |
| 135 | +[dynamically sized type]: dynamically-sized-types.html |
| 136 | +[Function pointers]: types.html#function-pointer-types |
| 137 | +[function item types]: types.html#function-item-types |
| 138 | +[implementation items]: items.html/implementations.html |
| 139 | +[indexing expressions]: expressions/array-expr.html#array-and-slice-indexing-expressions |
| 140 | +[interior mutability]: iterior-mutability.html |
| 141 | +[lang items]: attributes.html#language-items |
| 142 | +[Numeric types]: types.html#numeric-types |
| 143 | +[Methods]: items.html#associated-functions-and-methods |
| 144 | +[method resolution]: expressions/method-call-expr.html |
| 145 | +[operators]: expressions/operator-expr.html |
| 146 | +[orphan rules]: items/implementations.html#trait-implementation-coherence |
| 147 | +[Raw pointers]: types.html#raw-pointers-const-and-mut |
| 148 | +[`static` items]: items/static-items.html |
| 149 | +[Shared references]: types.html#shared-references- |
| 150 | +[the standard library]: ../std/index.html |
| 151 | +[trait object]: types.html#trait-objects |
| 152 | +[Tuples]: types.html#tuple-types |
| 153 | +[Type parameters]: types.html#type-parameters |
| 154 | +[variance]: ../nomicon/subtyping.html |
0 commit comments