@@ -178,9 +178,6 @@ The one value constructed by the associated [struct
178
178
expression] ( expressions.html#struct-expressions ) is the only value that
179
179
inhabits such a type.
180
180
181
- [ ^ structtype ] : ` struct ` types are analogous to ` struct ` types in C, the
182
- * record* types of the ML family, or the * struct* types of the Lisp family.
183
-
184
181
[ ^ structtype ] : ` struct ` types are analogous to ` struct ` types in C, the
185
182
* record* types of the ML family, or the * struct* types of the Lisp family.
186
183
@@ -202,6 +199,21 @@ corresponding `enum` type, as well as the size needed to store a discriminant.
202
199
Enum types cannot be denoted * structurally* as types, but must be denoted by
203
200
named reference to an [ ` enum ` item] ( items.html#enumerations ) .
204
201
202
+ [ ^ enumtype ] : The ` enum ` type is analogous to a ` data ` constructor declaration in
203
+ ML, or a * pick ADT* in Limbo.
204
+
205
+ ## Union types
206
+
207
+ A * union type* is a nominal, heterogeneous C-like union, denoted by the name of
208
+ a [ ` union ` item] ( items.html#unions ) .
209
+
210
+ A union contains the value of any one of its fields. Since the accessing the
211
+ wrong field can cause unexpected or undefined behaviour, it requires ` unsafe ` ,
212
+ except for initializing a union and writing ` Copy ` fields.
213
+
214
+ The memory layout of a ` union ` is undefined by default, but the ` #[repr(...)] `
215
+ attribute can be used to fix a layout.
216
+
205
217
[ ^ enumtype ] : The ` enum ` type is analogous to a ` data ` constructor declaration in
206
218
ML, or a * pick ADT* in Limbo.
207
219
@@ -252,6 +264,12 @@ operation: it involves only copying the pointer itself, that is, pointers are
252
264
referencing of a [ temporary value] ( expressions.html#temporary-lifetimes ) will
253
265
keep it alive during the scope of the reference itself.
254
266
267
+ ### Mutable references (` &mut ` )
268
+
269
+ These also point to memory owned by some other value. A mutable reference type
270
+ is written ` &mut type ` or ` &'a mut type ` . A mutable reference (that hasn't been
271
+ borrowed) is the only way to access the value it points to, so is not ` Copy ` .
272
+
255
273
### Raw pointers (` *const ` and ` *mut ` )
256
274
257
275
Raw pointers are pointers without safety or liveness guarantees. Raw pointers
@@ -490,6 +508,50 @@ objects of type `Ref<'a, SomeTrait>` are the same as `Ref<'a, (SomeTrait +
490
508
example in ` std::rc::Rc<T> ` .
491
509
492
510
511
+ ## Dynamically sized types
512
+
513
+ Most types have a fixed size that is known at compile time and implement the
514
+ trait [ ` Sized ` ] . A type with a size that is known only at run-time is called a
515
+ _ dynamically sized type_ (_ DST_ ) or unsized type. [ Slices] and [ trait objects]
516
+ are two of the most DSTs. Such types can only be used in certain cases:
517
+
518
+ * [ Pointer types] to DSTs are sized but have twice the size of pointers to
519
+ sized types
520
+ * Pointers to slices also store the number of elements of the slice.
521
+ * Pointers to trait objects also store a pointer to a vtable.
522
+ * Traits may be implemented for DSTs, this means that ` Self ` is not assumed to
523
+ implement ` Sized ` in trait definitions by default.
524
+ * DSTs can be used as type parameters with a bound of ` ?Sized `
525
+ * Structs may contain a DST as the last field, this makes the struct a DST as
526
+ well.
527
+
528
+ Notably: [ variables] , function parameters, [ const] and [ static] items must be
529
+ sized.
530
+
531
+ [ `Sized` ] : the-sized-trait.html
532
+ [ Slices ] : #array-and-slice-types
533
+ [ trait objects ] : #trait-objects
534
+ [ Pointer types ] : #pointer-types
535
+ [ variables ] : variables.html
536
+ [ const ] : items.html#constant-items
537
+ [ static ] : items.html#static-items
538
+
539
+ ## Interior mutability
540
+
541
+ References prevent a value being both aliased and mutated: mutable references
542
+ don't allow a value to be aliased and shared references prevent a value from
543
+ being mutated. Sometimes this is too restrictive. The type
544
+ [ ` std::cell::UnsafeCell<T> ` ] ( ../std/cell/struct.UnsafeCell.html ) provides an
545
+ unsafe API that allows a type to own a ` T ` that can be mutated through a shared
546
+ reference to the ` UnsafeCell ` (there is no change to mutable references).
547
+
548
+ The standard library provides a variety of different types that safely wrap
549
+ this functionality. For example ` std::cell::RefCell<T> ` uses run-time borrow
550
+ checks to ensure the usual rules around multiple references. The
551
+ ` std::sync::atomic ` module contains types that wrap a value that is only
552
+ accessed with atomic operations, allowing the value to be shared and mutated
553
+ across threads.
554
+
493
555
## Type parameters
494
556
495
557
Within the body of an item that has type parameter declarations, the names of
0 commit comments