1
1
# Types
2
2
3
+ > ** <sup >Syntax</sup >** \
4
+ > _ Type_ :\
5
+ >   ;  ;   ;  ; _ TypeCommon_ \
6
+ >   ;  ; | _ ParenthesizedType_   ; (` + ` [ _ TypeParamBounds_ ] )<sup >?</sup >\
7
+ >   ;  ; | [ _ TypePath_ ]   ; (` + ` [ _ TypeParamBounds_ ] )<sup >?</sup >\
8
+ >   ;  ; | [ _ ImplTraitType_ ] \
9
+ >   ;  ; | [ _ TraitObjectType_ ]
10
+ >
11
+ > _ TypeNoBounds_ :\
12
+ >   ;  ;   ;  ; _ TypeCommon_ \
13
+ >   ;  ; | _ ParenthesizedType_ \
14
+ >   ;  ; | [ _ TypePath_ ]
15
+ >
16
+ > _ TypeCommon_ :\
17
+ >   ;  ;   ;  ; [ _ TupleType_ ] \
18
+ >   ;  ; | [ _ NeverType_ ] \
19
+ >   ;  ; | [ _ RawPointerType_ ] \
20
+ >   ;  ; | [ _ ReferenceType_ ] \
21
+ >   ;  ; | [ _ ArrayType_ ] \
22
+ >   ;  ; | [ _ SliceType_ ] \
23
+ >   ;  ; | [ _ InferredType_ ] \
24
+ >   ;  ; | [ _ QualifiedPathInType_ ] \
25
+ >   ;  ; | [ _ BareFunctionType_ ]
26
+ >
27
+ > _ ParenthesizedType_ :\
28
+ >   ;  ;   ;  ; ` ( ` _ Type_ ` ) `
29
+
3
30
Every variable, item and value in a Rust program has a type. The _ type_ of a
4
31
* value* defines the interpretation of the memory holding it.
5
32
@@ -107,12 +134,21 @@ instantiated through a pointer type, such as `&str`.
107
134
108
135
## Never type
109
136
137
+ > ** <sup >Syntax</sup >** \
138
+ > _ NeverType_ : ` ! `
139
+
110
140
The never type ` ! ` is a type with no values, representing the result of
111
141
computations that never complete. Expressions of type ` ! ` can be coerced into
112
142
any other type.
113
143
114
144
## Tuple types
115
145
146
+ > ** <sup >Syntax</sup >** \
147
+ > _ TupleType_ :\
148
+ >   ;  ;   ;  ; ` ( ` ` ) ` \
149
+ >   ;  ; | ` ( ` [ _ Type_ ] ` , ` ` ) ` \
150
+ >   ;  ; | ` ( ` [ _ Type_ ]   ; ( ` , ` [ _ Type_ ] ) <sup >+</sup > ` , ` <sup >?</sup > ` ) `
151
+
116
152
A tuple * type* is a heterogeneous product of other types, called the * elements*
117
153
of the tuple. It has no nominal name and is instead structurally typed.
118
154
@@ -141,13 +177,21 @@ is often called ‘unit’ or ‘the unit type’.
141
177
142
178
## Array, and Slice types
143
179
144
- Rust has two different types for a list of items:
180
+ > ** <sup >Syntax</sup >** \
181
+ > _ ArrayType_ :\
182
+ >   ;  ; ` [ ` [ _ Type_ ] ` ; ` [ _ Expression_ ] ` ] `
183
+ >
184
+ > _ SliceType_ :\
185
+ >   ;  ; ` [ ` [ _ Type_ ] ` ] `
186
+
187
+ Rust has two different types for a list of items of the same type:
145
188
146
189
* ` [T; N] ` , an 'array'
147
190
* ` [T] ` , a 'slice'
148
191
149
192
An array has a fixed size, and can be allocated on either the stack or the
150
- heap.
193
+ heap. The size is an expression that evaluates to a
194
+ [ ` usize ` ] ( #machine-dependent-integer-types ) .
151
195
152
196
A slice is a [ dynamically sized type] representing a 'view' into an array. To
153
197
use a slice type it generally has to be used behind a pointer for example as
@@ -276,6 +320,10 @@ copied, stored into data structs, and returned from functions.
276
320
277
321
### Shared references (` & ` )
278
322
323
+ > ** <sup >Syntax</sup >** \
324
+ > _ ReferenceType_ :\
325
+ >   ;  ; ` & ` [ _ Lifetime_ ] <sup >?</sup > ` mut ` <sup >?</sup > [ _ TypeNoBounds_ ]
326
+
279
327
These point to memory _ owned by some other value_ . When a shared reference to a
280
328
value is created it prevents direct mutation of the value. [ Interior
281
329
mutability] ( interior-mutability.html ) provides an exception for this in certain
@@ -295,6 +343,10 @@ borrowed) is the only way to access the value it points to, so is not `Copy`.
295
343
296
344
### Raw pointers (` *const ` and ` *mut ` )
297
345
346
+ > ** <sup >Syntax</sup >** \
347
+ > _ RawPointerType_ :\
348
+ >   ;  ; ` * ` ( ` mut ` | ` const ` ) [ _ TypeNoBounds_ ]
349
+
298
350
Raw pointers are pointers without safety or liveness guarantees. Raw pointers
299
351
are written as ` *const T ` or ` *mut T ` , for example ` *const i32 ` means a raw
300
352
pointer to a 32-bit integer. Copying or dropping a raw pointer has no effect on
@@ -366,6 +418,16 @@ All function items implement [`Fn`], [`FnMut`], [`FnOnce`], [`Copy`],
366
418
367
419
## Function pointer types
368
420
421
+ > ** <sup >Syntax</sup >** \
422
+ > _ BareFunctionType_ :\
423
+ >   ;  ; [ _ ForLifetimes_ ] <sup >?</sup > [ _ FunctionFront_ ] ` fn ` \
424
+ >   ;  ;   ;  ; ` ( ` [ _ FunctionParametersMaybeNamedVariadic_ ] <sup >?</sup > ` ) ` _ BareFunctionReturnType_ <sup >?</sup >
425
+ >
426
+ > _ BareFunctionReturnType_ :\
427
+ >   ;  ; ` -> ` [ _ TypeNoBounds_ ]
428
+
429
+ <!-- TODO: _FunctionParametersMaybeNamedVariadic_ -->
430
+
369
431
Function pointer types, written using the ` fn ` keyword, refer to a function
370
432
whose identity is not necessarily known at compile-time. They can be created
371
433
via a coercion from both [ function items] ( #function-item-types ) and
@@ -560,7 +622,7 @@ Because captures are often by reference, the following general rules arise:
560
622
561
623
> ** <sup >Syntax</sup >** \
562
624
> _ TraitObjectType_ :\
563
- >   ;  ; ` dyn ` <sup >?</sup > _ TypeParamBounds_
625
+ >   ;  ; ` dyn ` <sup >?</sup > [ _ TypeParamBounds_ ]
564
626
565
627
A * trait object* is an opaque value of another type that implements a set of
566
628
traits. The set of traits is made up of an [ object safe] * base trait* plus any
@@ -658,6 +720,24 @@ inferred with a sensible choice.
658
720
659
721
[ defaults ] : lifetime-elision.html#default-trait-object-lifetimes
660
722
723
+ ## Inferred type
724
+ > ** <sup >Syntax</sup >** \
725
+ > _ InferredType_ : ` _ `
726
+
727
+ The inferred type asks the compiler to infer the type if possible based on the
728
+ surrounding information available. It cannot be used in item signatures. It is
729
+ often used in generic arguments:
730
+
731
+ ``` rust
732
+ let x : Vec <_ > = (0 .. 10 ). collect ();
733
+ ```
734
+
735
+ <!--
736
+ What else should be said here?
737
+ The only documentation I am aware of is https://rust-lang-nursery.github.io/rustc-guide/type-inference.html
738
+ Should there be a broader discussion of type inference somewhere?
739
+ -->
740
+
661
741
## Type parameters
662
742
663
743
Within the body of an item that has type parameter declarations, the names of
@@ -678,7 +758,12 @@ fn to_vec<A: Clone>(xs: &[A]) -> Vec<A> {
678
758
Here, ` first ` has type ` A ` , referring to ` to_vec ` 's ` A ` type parameter; and
679
759
` rest ` has type ` Vec<A> ` , a vector with element type ` A ` .
680
760
681
- ## Anonymous type parameters
761
+ ## Impl trait
762
+
763
+ > ** <sup >Syntax</sup >** \
764
+ > _ ImplTraitType_ : ` impl ` [ _ TypeParamBounds_ ]
765
+
766
+ ### Anonymous type parameters
682
767
683
768
> Note: This section is a placeholder for more comprehensive reference
684
769
> material.
@@ -692,7 +777,7 @@ bounds of the anonymous type parameter.
692
777
693
778
They are written as ` impl ` followed by a set of trait bounds.
694
779
695
- ## Abstract return types
780
+ ### Abstract return types
696
781
697
782
> Note: This section is a placeholder for more comprehensive reference
698
783
> material.
@@ -740,6 +825,27 @@ impl Printable for String {
740
825
741
826
> Note: The notation ` &self ` is a shorthand for ` self: &Self ` .
742
827
828
+ [ _ArrayType_ ] : #array-and-slice-types
829
+ [ _BareFunctionType_ ] : #function-pointer-types
830
+ [ _Expression_ ] : expressions.html
831
+ [ _ForLifetimes_ ] : items/generics.html#where-clauses
832
+ [ _FunctionFront_ ] : items/functions.html
833
+ [ _FunctionParametersMaybeNamed_ ] : items/functions.html
834
+ [ _ImplTraitType_ ] : #impl-trait
835
+ [ _InferredType_ ] : #inferred-type
836
+ [ _Lifetime_ ] : trait-bounds.html
837
+ [ _NeverType_ ] : #never-type
838
+ [ _ParenthesizedType_ ] : #parenthesized-type
839
+ [ _QualifiedPathInType_ ] : paths.html#qualified-paths
840
+ [ _RawPointerType_ ] : #raw-pointers-const-and-mut
841
+ [ _ReferenceType_ ] : #shared-references-
842
+ [ _SliceType_ ] : #array-and-slice-types
843
+ [ _TraitObjectType_ ] : #trait-objects
844
+ [ _TupleType_ ] : #tuple-types
845
+ [ _TypeNoBounds_ ] : #types
846
+ [ _TypeParamBounds_ ] : trait-bounds.html
847
+ [ _TypePath_ ] : paths.html#paths-in-types
848
+ [ _Type_ ] : #types
743
849
[ `Fn` ] : ../std/ops/trait.Fn.html
744
850
[ `FnMut` ] : ../std/ops/trait.FnMut.html
745
851
[ `FnOnce` ] : ../std/ops/trait.FnOnce.html
0 commit comments