@@ -9,15 +9,17 @@ limited capabilities.
9
9
10
10
## Primitive types
11
11
12
- Some types are defined by the language, rather than as part of the standard library,
13
- these are called _ primitive types_ . Some of these are induvidual types:
12
+ Some types are defined by the language, rather than as part of the standard
13
+ library, these are called _ primitive types_ . Some of these are induvidual
14
+ types:
14
15
15
16
* The boolean type ` bool ` with values ` true ` and ` false ` .
16
17
* The [ machine types] (integer and floating-point).
17
18
* The [ machine-dependent integer types] .
18
19
* The [ textual types] ` char ` and ` str ` .
19
20
20
- There are also some primitive constructs for generic types built in to the language:
21
+ There are also some primitive constructs for generic types built in to the
22
+ language:
21
23
22
24
* [ Tuples]
23
25
* [ Arrays]
@@ -45,27 +47,27 @@ There are also some primitive constructs for generic types built in to the langu
45
47
The machine types are the following:
46
48
47
49
* The unsigned word types ` u8 ` , ` u16 ` , ` u32 ` and ` u64 ` , with values drawn from
48
- the integer intervals [ 0, 2^8 - 1] , [ 0, 2^16 - 1] , [ 0, 2^32 - 1] and
49
- [ 0, 2^64 - 1] respectively.
50
+ the integer intervals [ 0, 2^8 - 1] , [ 0, 2^16 - 1] , [ 0, 2^32 - 1] and [ 0, 2^64
51
+ - 1] respectively.
50
52
51
53
* The signed two's complement word types ` i8 ` , ` i16 ` , ` i32 ` and ` i64 ` , with
52
- values drawn from the integer intervals [ -(2^(7)), 2^7 - 1] ,
53
- [ -(2^(15)), 2^15 - 1] , [ -(2^(31)), 2^31 - 1] , [ -(2^(63)), 2^63 - 1]
54
- respectively.
54
+ values drawn from the integer intervals [ -(2^(7)), 2^7 - 1] , [ -(2^(15)), 2^15
55
+ - 1] , [ -(2^(31)), 2^31 - 1] , [ -(2^(63)), 2^63 - 1] respectively.
55
56
56
57
* The IEEE 754-2008 ` binary32 ` and ` binary64 ` floating-point types: ` f32 ` and
57
58
` f64 ` , respectively.
58
59
59
60
### Machine-dependent integer types
60
61
61
- The ` usize ` type is an unsigned integer type with the same number of bits as the
62
- platform's pointer type. It can represent every memory address in the process.
62
+ The ` usize ` type is an unsigned integer type with the same number of bits as
63
+ the platform's pointer type. It can represent every memory address in the
64
+ process.
63
65
64
66
The ` isize ` type is a signed integer type with the same number of bits as the
65
67
platform's pointer type. The theoretical upper bound on object and array size
66
- is the maximum ` isize ` value. This ensures that ` isize ` can be used to calculate
67
- differences between pointers into an object or array and can address every byte
68
- within an object along with one byte past the end.
68
+ is the maximum ` isize ` value. This ensures that ` isize ` can be used to
69
+ calculate differences between pointers into an object or array and can address
70
+ every byte within an object along with one byte past the end.
69
71
70
72
## Textual types
71
73
@@ -79,8 +81,8 @@ string.
79
81
80
82
A value of type ` str ` is a Unicode string, represented as an array of 8-bit
81
83
unsigned bytes holding a sequence of UTF-8 code points. Since ` str ` is a
82
- [ dynamically sized type] , it is not a _ first-class_ type, but can only be instantiated
83
- through a pointer type, such as ` &str ` .
84
+ [ dynamically sized type] , it is not a _ first-class_ type, but can only be
85
+ instantiated through a pointer type, such as ` &str ` .
84
86
85
87
## Tuple types
86
88
@@ -91,8 +93,8 @@ Tuple types and values are denoted by listing the types or values of their
91
93
elements, respectively, in a parenthesized, comma-separated list.
92
94
93
95
Because tuple elements don't have a name, they can only be accessed by
94
- pattern-matching or by using ` N ` directly as a field to access the
95
- ` N ` th element.
96
+ pattern-matching or by using ` N ` directly as a field to access the ` N ` th
97
+ element.
96
98
97
99
An example of a tuple type and its use:
98
100
@@ -160,25 +162,24 @@ expression](expressions.html#struct-expressions).
160
162
161
163
The memory layout of a ` struct ` is undefined by default to allow for compiler
162
164
optimizations like field reordering, but it can be fixed with the
163
- ` #[repr(...)] ` attribute. In either case, fields may be given in any order in
164
- 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
165
167
have the same memory layout.
166
168
167
169
The fields of a ` struct ` may be qualified by [ visibility
168
- modifiers] ( visibility-and-privacy.html ) , to allow access to data in a
169
- struct outside a module.
170
+ modifiers] ( visibility-and-privacy.html ) , to allow access to data in a struct
171
+ outside a module.
170
172
171
173
A _ tuple struct_ type is just like a struct type, except that the fields are
172
174
anonymous.
173
175
174
- A _ unit-like struct_ type is like a struct type, except that it has no
175
- fields. The one value constructed by the associated [ struct
176
- expression] ( expressions.html#struct-expressions ) is the only value that inhabits such a
177
- type.
176
+ A _ unit-like struct_ type is like a struct type, except that it has no fields.
177
+ The one value constructed by the associated [ struct
178
+ expression] ( expressions.html#struct-expressions ) is the only value that
179
+ inhabits such a type.
178
180
179
- [ ^ structtype ] : ` struct ` types are analogous to ` struct ` types in C,
180
- the * record* types of the ML family,
181
- or the * struct* types of the Lisp family.
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.
182
183
183
184
## Enumerated types
184
185
@@ -203,8 +204,8 @@ named reference to an [`enum` item](items.html#enumerations).
203
204
204
205
## Union types
205
206
206
- A * union type* is a nominal, heterogeneous C-like union, denoted by the name
207
- of a [ ` union ` item] ( items.html#unions ) .
207
+ A * union type* is a nominal, heterogeneous C-like union, denoted by the name of
208
+ a [ ` union ` item] ( items.html#unions ) .
208
209
209
210
A union contains the value of any one of its fields. Since the accessing the
210
211
wrong field can cause unexpected or undefined behaviour, it requires ` unsafe ` ,
@@ -221,13 +222,14 @@ recursive. That is, each `enum` variant or `struct` or `union` field may refer,
221
222
directly or indirectly, to the enclosing ` enum ` or ` struct ` type itself. Such
222
223
recursion has restrictions:
223
224
224
- * Recursive types must include a nominal type in the recursion
225
- (not mere [ type definitions] ( ../grammar.html#type-definitions ) ,
226
- or other structural types such as [ arrays] ( #array-and-slice-types ) or [ tuples] ( #tuple-types ) ).
227
- * The size of a recursive type must be finite;
228
- in other words the recursive fields of the type must be [ pointer types] ( #pointer-types ) .
229
- * Recursive type definitions can cross module boundaries, but not module * visibility* boundaries,
230
- or crate boundaries (in order to simplify the module system and type checker).
225
+ * Recursive types must include a nominal type in the recursion (not mere [ type
226
+ definitions] ( ../grammar.html#type-definitions ) , or other structural types
227
+ such as [ arrays] ( #array-and-slice-types ) or [ tuples] ( #tuple-types ) ).
228
+ * The size of a recursive type must be finite; in other words the recursive
229
+ fields of the type must be [ pointer types] ( #pointer-types ) .
230
+ * Recursive type definitions can cross module boundaries, but not module
231
+ * visibility* boundaries, or crate boundaries (in order to simplify the module
232
+ system and type checker).
231
233
232
234
An example of a * recursive* type and its use:
233
235
@@ -243,8 +245,8 @@ let a: List<i32> = List::Cons(7, Box::new(List::Cons(13, Box::new(List::Nil))));
243
245
## Pointer types
244
246
245
247
All pointers in Rust are explicit first-class values. They can be copied,
246
- stored into data structs, and returned from functions. There are two
247
- varieties of pointer in Rust:
248
+ stored into data structs, and returned from functions. There are two varieties
249
+ of pointer in Rust:
248
250
249
251
### Shared references (` & ` )
250
252
@@ -261,10 +263,9 @@ keep it alive during the scope of the reference itself.
261
263
262
264
### Mutable references (` &mut ` )
263
265
264
- These also point to memory owned by some other value. A mutable reference
265
- type is written ` &mut type ` or ` &'a mut type ` . A mutable reference (that
266
- hasn't been borrowed) is the only way to access the value it points to, so
267
- is not ` Copy ` .
266
+ These also point to memory owned by some other value. A mutable reference type
267
+ is written ` &mut type ` or ` &'a mut type ` . A mutable reference (that hasn't been
268
+ borrowed) is the only way to access the value it points to, so is not ` Copy ` .
268
269
269
270
### Raw pointers (` *const ` and ` *mut ` )
270
271
@@ -283,8 +284,8 @@ types](#dynamically-sized-types) they also have their addition data compared.
283
284
284
285
### Smart Pointers
285
286
286
- The standard library contains additional 'smart pointer' types beyond references
287
- and raw pointers.
287
+ The standard library contains additional 'smart pointer' types beyond
288
+ references and raw pointers.
288
289
289
290
## Function item types
290
291
@@ -297,8 +298,8 @@ need to contain an actual function pointer, and no indirection is needed when
297
298
the function is called.
298
299
299
300
There is currently no syntax that directly refers to a function item type, but
300
- the compiler will display the type as something like ` fn() {foo::<u32>} ` in error
301
- messages.
301
+ the compiler will display the type as something like ` fn() {foo::<u32>} ` in
302
+ error messages.
302
303
303
304
Because the function item type explicitly identifies the function, the item
304
305
types of different functions - different items, or the same item with different
@@ -310,11 +311,11 @@ let x = &mut foo::<i32>;
310
311
*x = foo::<u32>; //~ ERROR mismatched types
311
312
```
312
313
313
- However, there is a [ coercion] from function items to [ function pointers ] ( #function-pointer-types )
314
- with the same signature, which is triggered not only when a function item
315
- is used when a function pointer is directly expected, but also when different
316
- function item types with the same signature meet in different arms of the same
317
- ` if ` or ` match ` :
314
+ However, there is a [ coercion] from function items to [ function
315
+ pointers ] ( #function-pointer-types ) with the same signature, which is triggered
316
+ not only when a function item is used when a function pointer is directly
317
+ expected, but also when different function item types with the same signature
318
+ meet in different arms of the same ` if ` or ` match ` :
318
319
319
320
[ coercion ] : type-coercions.html
320
321
@@ -363,29 +364,26 @@ x = bo(5,7);
363
364
A [ closure expression] ( expressions.html#closure-expressions ) produces a closure
364
365
value with a unique, anonymous type that cannot be written out.
365
366
366
- Depending on the requirements of the closure, its type implements one or
367
- more of the closure traits:
367
+ Depending on the requirements of the closure, its type implements one or more
368
+ of the closure traits:
368
369
369
- * ` FnOnce `
370
- : The closure can be called once. A closure called as ` FnOnce `
371
- can move out values from its environment.
370
+ * ` FnOnce ` : The closure can be called once. A closure called as ` FnOnce ` can
371
+ move out values from its environment.
372
372
373
- * ` FnMut `
374
- : The closure can be called multiple times as mutable. A closure called as
375
- ` FnMut ` can mutate values from its environment. ` FnMut ` inherits from
376
- ` FnOnce ` (i.e. anything implementing ` FnMut ` also implements ` FnOnce ` ).
373
+ * ` FnMut ` : The closure can be called multiple times as mutable. A closure
374
+ called as ` FnMut ` can mutate values from its environment. ` FnMut ` inherits
375
+ from ` FnOnce ` (i.e. anything implementing ` FnMut ` also implements ` FnOnce ` ).
377
376
378
- * ` Fn `
379
- : The closure can be called multiple times through a shared reference. A
380
- closure called as ` Fn ` can neither move out from nor mutate captured
381
- variables, but read-only access to such values is allowed. Using ` move ` to
382
- capture variables by value is allowed so long as they aren't mutated or
383
- moved in the body of the closure. ` Fn ` inherits from ` FnMut ` , which itself
384
- inherits from ` FnOnce ` .
377
+ * ` Fn ` : The closure can be called multiple times through a shared reference. A
378
+ closure called as ` Fn ` can neither move out from nor mutate captured
379
+ variables, but read-only access to such values is allowed. Using ` move ` to
380
+ capture variables by value is allowed so long as they aren't mutated or moved
381
+ in the body of the closure. ` Fn ` inherits from ` FnMut ` , which itself inherits
382
+ from ` FnOnce ` .
385
383
386
- Closures that don't use anything from their environment ("non capturing closures")
387
- can be coerced to function pointers (` fn ` ) with the matching signature.
388
- To adopt the example from the section above:
384
+ Closures that don't use anything from their environment ("non capturing
385
+ closures") can be coerced to function pointers (` fn ` ) with the matching
386
+ signature. To adopt the example from the section above:
389
387
390
388
``` rust
391
389
let add = | x , y | x + y ;
@@ -414,7 +412,8 @@ function pointer is loaded from the trait object vtable and invoked indirectly.
414
412
The actual implementation for each vtable entry can vary on an object-by-object
415
413
basis.
416
414
417
- Note that for a trait object types only exist for _ object-safe_ traits ([ RFC 255] ):
415
+ Note that for a trait object types only exist for _ object-safe_ traits ([ RFC
416
+ 255] ):
418
417
419
418
[ RFC 255 ] : https://github.com/rust-lang/rfcs/blob/master/text/0255-object-safety.md
420
419
@@ -457,8 +456,8 @@ type signature of `print`, and the cast expression in `main`.
457
456
458
457
Since a trait object can contain references, the lifetimes of those references
459
458
need to be expressed as part of the trait object. The assumed lifetime of
460
- references held by a trait object is called its _ default object lifetime bound _ .
461
- These were defined in [ RFC 599] and amended in [ RFC 1156] .
459
+ references held by a trait object is called its _ default object lifetime
460
+ bound _ . These were defined in [ RFC 599] and amended in [ RFC 1156] .
462
461
463
462
[ RFC 599 ] : https://github.com/rust-lang/rfcs/blob/master/text/0599-default-object-bound.md
464
463
[ RFC 1156 ] : https://github.com/rust-lang/rfcs/blob/master/text/1156-adjust-default-object-bounds.md
@@ -480,8 +479,9 @@ Box<Foo + 'static>
480
479
```
481
480
482
481
The ` + 'static ` and ` + 'a ` refer to the default bounds of those kinds of trait
483
- objects, and also to how you can directly override them. Note that the innermost
484
- object sets the bound, so ` &'a Box<Foo> ` is still ` &'a Box<Foo + 'static> ` .
482
+ objects, and also to how you can directly override them. Note that the
483
+ innermost object sets the bound, so ` &'a Box<Foo> ` is still `&'a Box<Foo +
484
+ 'static>`.
485
485
486
486
For traits that have lifetime parameters of their own, the default bound is
487
487
based on that lifetime parameter:
@@ -508,7 +508,8 @@ trait [`Sized`]. A type with a size that is known only at run-time is called a
508
508
_ dynamically sized type_ (_ DST_ ) or unsized type. [ Slices] and [ trait objects]
509
509
are two of the most DSTs. Such types can only be used in certain cases:
510
510
511
- * [ Pointer types] to DSTs are sized but have twice the size of pointers to sized types
511
+ * [ Pointer types] to DSTs are sized but have twice the size of pointers to
512
+ sized types
512
513
* Pointers to slices also store the number of elements of the slice.
513
514
* Pointers to trait objects also store a pointer to a vtable.
514
515
* Traits may be implemented for DSTs
@@ -560,14 +561,15 @@ fn to_vec<A: Clone>(xs: &[A]) -> Vec<A> {
560
561
}
561
562
```
562
563
563
- Here, ` first ` has type ` A ` , referring to ` to_vec ` 's ` A ` type parameter; and ` rest `
564
- has type ` Vec<A> ` , a vector with element type ` A ` .
564
+ Here, ` first ` has type ` A ` , referring to ` to_vec ` 's ` A ` type parameter; and
565
+ ` rest ` has type ` Vec<A> ` , a vector with element type ` A ` .
565
566
566
567
## Self types
567
568
568
- The special type ` Self ` has a meaning within traits and impls. In a trait definition, it refers
569
- to an implicit type parameter representing the "implementing" type. In an impl,
570
- it is an alias for the implementing type. For example, in:
569
+ The special type ` Self ` has a meaning within traits and impls. In a trait
570
+ definition, it refers to an implicit type parameter representing the
571
+ "implementing" type. In an impl, it is an alias for the implementing type. For
572
+ example, in:
571
573
572
574
``` rust
573
575
pub trait From <T > {
@@ -581,8 +583,8 @@ impl From<i32> for String {
581
583
}
582
584
```
583
585
584
- The notation ` Self ` in the impl refers to the implementing type: ` String ` . In another
585
- example:
586
+ The notation ` Self ` in the impl refers to the implementing type: ` String ` . In
587
+ another example:
586
588
587
589
``` rust
588
590
trait Printable {
0 commit comments