@@ -93,8 +93,8 @@ Tuple types and values are denoted by listing the types or values of their
93
93
elements, respectively, in a parenthesized, comma-separated list.
94
94
95
95
Because tuple elements don't have a name, they can only be accessed by
96
- pattern-matching or by using ` N ` directly as a field to access the
97
- ` N ` th element.
96
+ pattern-matching or by using ` N ` directly as a field to access the ` N ` th
97
+ element.
98
98
99
99
An example of a tuple type and its use:
100
100
@@ -149,7 +149,7 @@ array or slice is always bounds-checked in safe methods and operators.
149
149
The [ ` Vec<T> ` ] standard library type provides a heap allocated resizable array
150
150
type.
151
151
152
- [ dynamically sized type ] : # dynamically-sized-types
152
+ [ dynamically sized type ] : dynamically-sized-types.html
153
153
[ `Vec<T>` ] : ../std/vec/struct.Vec.html
154
154
155
155
## Struct types
@@ -162,13 +162,13 @@ expression](expressions.html#struct-expressions).
162
162
163
163
The memory layout of a ` struct ` is undefined by default to allow for compiler
164
164
optimizations like field reordering, but it can be fixed with the
165
- ` #[repr(...)] ` attribute. In either case, fields may be given in any order in
166
- a corresponding struct * expression* ; the resulting ` struct ` value will always
165
+ ` #[repr(...)] ` attribute. In either case, fields may be given in any order in a
166
+ corresponding struct * expression* ; the resulting ` struct ` value will always
167
167
have the same memory layout.
168
168
169
169
The fields of a ` struct ` may be qualified by [ visibility
170
- modifiers] ( visibility-and-privacy.html ) , to allow access to data in a
171
- struct outside a module.
170
+ modifiers] ( visibility-and-privacy.html ) , to allow access to data in a struct
171
+ outside a module.
172
172
173
173
A _ tuple struct_ type is just like a struct type, except that the fields are
174
174
anonymous.
@@ -202,6 +202,21 @@ named reference to an [`enum` item](items.html#enumerations).
202
202
[ ^ enumtype ] : The ` enum ` type is analogous to a ` data ` constructor declaration in
203
203
ML, or a * pick ADT* in Limbo.
204
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, ` unsafe ` is required
212
+ to read from a union field or to write to a field that doesn't implement
213
+ [ ` Copy ` ] .
214
+
215
+ The memory layout of a ` union ` is undefined by default, but the ` #[repr(...)] `
216
+ attribute can be used to fix a layout.
217
+
218
+ [ `Copy` ] : the-copy-trait.html
219
+
205
220
## Recursive types
206
221
207
222
Nominal types &mdash ; [ structs] ( #struct-types ) ,
@@ -240,7 +255,7 @@ copied, stored into data structs, and returned from functions.
240
255
241
256
These point to memory _ owned by some other value_ . When a shared reference to a
242
257
value is created it prevents direct mutation of the value. [ Interior
243
- mutability] ( # interior-mutability) provides an exception for this in certain
258
+ mutability] ( interior-mutability.html ) provides an exception for this in certain
244
259
circumstances. As the name suggests, any number of shared references to a value
245
260
may exit. A shared reference type is written ` &type ` , or ` &'a type ` when you
246
261
need to specify an explicit lifetime. Copying a reference is a "shallow"
@@ -249,6 +264,12 @@ operation: it involves only copying the pointer itself, that is, pointers are
249
264
referencing of a [ temporary value] ( expressions.html#temporary-lifetimes ) will
250
265
keep it alive during the scope of the reference itself.
251
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
+
252
273
### Raw pointers (` *const ` and ` *mut ` )
253
274
254
275
Raw pointers are pointers without safety or liveness guarantees. Raw pointers
@@ -262,7 +283,8 @@ foreign code, and writing performance-critical or low-level functions.
262
283
263
284
When comparing pointers they are compared by their address, rather than by what
264
285
they point to. When comparing pointers to [ dynamically sized
265
- types] ( #dynamically-sized-types ) they also have their addition data compared.
286
+ types] ( dynamically-sized-types.html ) they also have their addition data
287
+ compared.
266
288
267
289
### Smart Pointers
268
290
@@ -365,9 +387,9 @@ more of the closure traits:
365
387
moved in the body of the closure. ` Fn ` inherits from ` FnMut ` , which itself
366
388
inherits from ` FnOnce ` .
367
389
368
- Closures that don't use anything from their environment ("non capturing closures")
369
- can be coerced to function pointers (` fn ` ) with the matching signature.
370
- To adopt the example from the section above:
390
+ Closures that don't use anything from their environment ("non capturing
391
+ closures") can be coerced to function pointers (` fn ` ) with the matching
392
+ signature. To adopt the example from the section above:
371
393
372
394
``` rust
373
395
let add = | x , y | x + y ;
@@ -382,9 +404,11 @@ x = bo(5,7);
382
404
## Trait objects
383
405
384
406
In Rust, trait names also refer to [ dynamically sized types] called _ trait
385
- objects_ . Like all DSTs, trait objects are used behind some kind of pointer:
386
- ` &SomeTrait ` or ` Box<SomeTrait> ` . Each instance of a pointer to a trait object
387
- includes:
407
+ objects_ . Like all <abbr title =" dynamically sized types " >DSTs</abbr >, trait
408
+ objects are used behind some kind of pointer: ` &SomeTrait ` or ` Box<SomeTrait> ` .
409
+ Each instance of a pointer to a trait object includes:
410
+
411
+ [ dynamically sized types ] : dynamically-sized-types.html
388
412
389
413
- a pointer to an instance of a type ` T ` that implements ` SomeTrait `
390
414
- a _ virtual method table_ , often just called a _ vtable_ , which contains, for
@@ -464,11 +488,11 @@ Box<Foo + 'static>
464
488
impl Foo {}
465
489
impl Foo + 'static {}
466
490
467
- // ...so are these as &'a T requires T: 'a
491
+ // ...so are these, because &'a T requires T: 'a
468
492
&'a Foo
469
493
&'a (Foo + 'a)
470
494
471
- // ... std::cell::Ref<'a, T> also requires T: 'a, so these are also the same
495
+ // std::cell::Ref<'a, T> also requires T: 'a, so these are the same
472
496
std::cell::Ref<'a, Foo>
473
497
std::cell::Ref<'a, Foo + 'a>
474
498
0 commit comments