1
1
# Types
2
2
3
+ > ** <sup >Syntax</sup >** \
4
+ > _ Type_ :\
5
+ >   ;  ;   ;  ; _ TypeNoBounds_ \
6
+ >   ;  ; | [ _ ImplTraitType_ ] \
7
+ >   ;  ; | [ _ TraitObjectType_ ]
8
+ >
9
+ > _ TypeNoBounds_ :\
10
+ >   ;  ;   ;  ; [ _ ParenthesizedType_ ] \
11
+ >   ;  ; | [ _ ImplTraitTypeOneBound_ ] \
12
+ >   ;  ; | [ _ TraitObjectTypeOneBound_ ] \
13
+ >   ;  ; | [ _ TypePath_ ] \
14
+ >   ;  ; | [ _ TupleType_ ] \
15
+ >   ;  ; | [ _ NeverType_ ] \
16
+ >   ;  ; | [ _ RawPointerType_ ] \
17
+ >   ;  ; | [ _ ReferenceType_ ] \
18
+ >   ;  ; | [ _ ArrayType_ ] \
19
+ >   ;  ; | [ _ SliceType_ ] \
20
+ >   ;  ; | [ _ InferredType_ ] \
21
+ >   ;  ; | [ _ QualifiedPathInType_ ] \
22
+ >   ;  ; | [ _ BareFunctionType_ ]
23
+
3
24
Every variable, item and value in a Rust program has a type. The _ type_ of a
4
25
* value* defines the interpretation of the memory holding it.
5
26
@@ -107,12 +128,20 @@ instantiated through a pointer type, such as `&str`.
107
128
108
129
## Never type
109
130
131
+ > ** <sup >Syntax</sup >** \
132
+ > _ NeverType_ : ` ! `
133
+
110
134
The never type ` ! ` is a type with no values, representing the result of
111
135
computations that never complete. Expressions of type ` ! ` can be coerced into
112
136
any other type.
113
137
114
138
## Tuple types
115
139
140
+ > ** <sup >Syntax</sup >** \
141
+ > _ TupleType_ :\
142
+ >   ;  ;   ;  ; ` ( ` ` ) ` \
143
+ >   ;  ; | ` ( ` ( [ _ Type_ ] ` , ` )<sup >+</sup > [ _ Type_ ] <sup >?</sup > ` ) `
144
+
116
145
A tuple * type* is a heterogeneous product of other types, called the * elements*
117
146
of the tuple. It has no nominal name and is instead structurally typed.
118
147
@@ -139,15 +168,41 @@ assert_eq!(p.1, "ten");
139
168
For historical reasons and convenience, the tuple type with no elements (` () ` )
140
169
is often called ‘unit’ or ‘the unit type’.
141
170
171
+ ## Parenthesized types
172
+
173
+ > _ ParenthesizedType_ :\
174
+ >   ;  ; ` ( ` [ _ Type_ ] ` ) `
175
+
176
+ In some situations the combination of types may be ambiguous. Use parentheses
177
+ around a type to avoid ambiguity. For example, the ` + ` operator for [ type
178
+ boundaries] within a [ reference type] [ _ReferenceType_ ] is unclear where the
179
+ boundary applies, so the use of parentheses is required. Grammar rules that
180
+ require this disambiguation use the [ _ TypeNoBounds_ ] rule instead of
181
+ [ _ Type_ ] .
182
+
183
+
184
+ ``` rust
185
+ # use std :: any :: Any ;
186
+ type T <'a > = & 'a (Any + Send );
187
+ ```
188
+
142
189
## Array, and Slice types
143
190
144
- Rust has two different types for a list of items:
191
+ > ** <sup >Syntax</sup >** \
192
+ > _ ArrayType_ :\
193
+ >   ;  ; ` [ ` [ _ Type_ ] ` ; ` [ _ Expression_ ] ` ] `
194
+ >
195
+ > _ SliceType_ :\
196
+ >   ;  ; ` [ ` [ _ Type_ ] ` ] `
197
+
198
+ Rust has two different types for a list of items of the same type:
145
199
146
200
* ` [T; N] ` , an 'array'
147
201
* ` [T] ` , a 'slice'
148
202
149
203
An array has a fixed size, and can be allocated on either the stack or the
150
- heap.
204
+ heap. The size is an expression that evaluates to a
205
+ [ ` usize ` ] ( #machine-dependent-integer-types ) .
151
206
152
207
A slice is a [ dynamically sized type] representing a 'view' into an array. To
153
208
use a slice type it generally has to be used behind a pointer for example as
@@ -276,6 +331,10 @@ copied, stored into data structs, and returned from functions.
276
331
277
332
### Shared references (` & ` )
278
333
334
+ > ** <sup >Syntax</sup >** \
335
+ > _ ReferenceType_ :\
336
+ >   ;  ; ` & ` [ _ Lifetime_ ] <sup >?</sup > ` mut ` <sup >?</sup > [ _ TypeNoBounds_ ]
337
+
279
338
These point to memory _ owned by some other value_ . When a shared reference to a
280
339
value is created it prevents direct mutation of the value. [ Interior
281
340
mutability] ( interior-mutability.html ) provides an exception for this in certain
@@ -295,6 +354,10 @@ borrowed) is the only way to access the value it points to, so is not `Copy`.
295
354
296
355
### Raw pointers (` *const ` and ` *mut ` )
297
356
357
+ > ** <sup >Syntax</sup >** \
358
+ > _ RawPointerType_ :\
359
+ >   ;  ; ` * ` ( ` mut ` | ` const ` ) [ _ TypeNoBounds_ ]
360
+
298
361
Raw pointers are pointers without safety or liveness guarantees. Raw pointers
299
362
are written as ` *const T ` or ` *mut T ` , for example ` *const i32 ` means a raw
300
363
pointer to a 32-bit integer. Copying or dropping a raw pointer has no effect on
@@ -366,6 +429,26 @@ All function items implement [`Fn`], [`FnMut`], [`FnOnce`], [`Copy`],
366
429
367
430
## Function pointer types
368
431
432
+ > ** <sup >Syntax</sup >** \
433
+ > _ BareFunctionType_ :\
434
+ >   ;  ; [ _ ForLifetimes_ ] <sup >?</sup > [ _ FunctionFront_ ] ` fn ` \
435
+ >   ;  ;   ;  ; ` ( ` _ FunctionParametersMaybeNamedVariadic_ <sup >?</sup > ` ) ` _ BareFunctionReturnType_ <sup >?</sup >
436
+ >
437
+ > _ BareFunctionReturnType_ :\
438
+ >   ;  ; ` -> ` [ _ TypeNoBounds_ ]
439
+ >
440
+ > _ FunctionParametersMaybeNamedVariadic_ :\
441
+ >   ;  ; _ MaybeNamedFunctionParameters_ | _ MaybeNamedFunctionParametersVariadic_
442
+ >
443
+ > _ MaybeNamedFunctionParameters_ :\
444
+ >   ;  ; _ MaybeNamedParam_ ( ` , ` _ MaybeNamedParam_ )<sup >\* </sup > ` , ` <sup >?</sup >
445
+ >
446
+ > _ MaybeNamedParam_ :\
447
+ >   ;  ; ( ( [ IDENTIFIER] | ` _ ` ) ` : ` )<sup >?</sup > [ _ Type_ ]
448
+ >
449
+ > _ MaybeNamedFunctionParametersVariadic_ :\
450
+ >   ;  ; ( _ MaybeNamedParam_ ` , ` )<sup >\* </sup > _ MaybeNamedParam_ ` , ` ` ... `
451
+
369
452
Function pointer types, written using the ` fn ` keyword, refer to a function
370
453
whose identity is not necessarily known at compile-time. They can be created
371
454
via a coercion from both [ function items] ( #function-item-types ) and
@@ -375,6 +458,9 @@ A function pointer type consists of a possibly-empty set of function-type
375
458
modifiers (such as ` unsafe ` or ` extern ` ), a sequence of input types and an
376
459
output type.
377
460
461
+ Variadic parameters can only be specified with [ ` extern ` ] function types with
462
+ the ` "C" ` or ` "cdecl" ` calling convention.
463
+
378
464
An example where ` Binop ` is defined as a function pointer type:
379
465
380
466
``` rust
@@ -560,7 +646,10 @@ Because captures are often by reference, the following general rules arise:
560
646
561
647
> ** <sup >Syntax</sup >** \
562
648
> _ TraitObjectType_ :\
563
- >   ;  ; ` dyn ` <sup >?</sup > _ TypeParamBounds_
649
+ >   ;  ; ` dyn ` <sup >?</sup > [ _ TypeParamBounds_ ]
650
+ >
651
+ > _ TraitObjectTypeOneBound_ :\
652
+ >   ;  ; ` dyn ` <sup >?</sup > [ _ TraitBound_ ]
564
653
565
654
A * trait object* is an opaque value of another type that implements a set of
566
655
traits. The set of traits is made up of an [ object safe] * base trait* plus any
@@ -658,6 +747,24 @@ inferred with a sensible choice.
658
747
659
748
[ defaults ] : lifetime-elision.html#default-trait-object-lifetimes
660
749
750
+ ## Inferred type
751
+ > ** <sup >Syntax</sup >** \
752
+ > _ InferredType_ : ` _ `
753
+
754
+ The inferred type asks the compiler to infer the type if possible based on the
755
+ surrounding information available. It cannot be used in item signatures. It is
756
+ often used in generic arguments:
757
+
758
+ ``` rust
759
+ let x : Vec <_ > = (0 .. 10 ). collect ();
760
+ ```
761
+
762
+ <!--
763
+ What else should be said here?
764
+ The only documentation I am aware of is https://rust-lang-nursery.github.io/rustc-guide/type-inference.html
765
+ There should be a broader discussion of type inference somewhere.
766
+ -->
767
+
661
768
## Type parameters
662
769
663
770
Within the body of an item that has type parameter declarations, the names of
@@ -678,7 +785,14 @@ fn to_vec<A: Clone>(xs: &[A]) -> Vec<A> {
678
785
Here, ` first ` has type ` A ` , referring to ` to_vec ` 's ` A ` type parameter; and
679
786
` rest ` has type ` Vec<A> ` , a vector with element type ` A ` .
680
787
681
- ## Anonymous type parameters
788
+ ## Impl trait
789
+
790
+ > ** <sup >Syntax</sup >** \
791
+ > _ ImplTraitType_ : ` impl ` [ _ TypeParamBounds_ ]
792
+ >
793
+ > _ ImplTraitTypeOneBound_ : ` impl ` [ _ TraitBound_ ]
794
+
795
+ ### Anonymous type parameters
682
796
683
797
> Note: This section is a placeholder for more comprehensive reference
684
798
> material.
@@ -692,7 +806,7 @@ bounds of the anonymous type parameter.
692
806
693
807
They are written as ` impl ` followed by a set of trait bounds.
694
808
695
- ## Abstract return types
809
+ ### Abstract return types
696
810
697
811
> Note: This section is a placeholder for more comprehensive reference
698
812
> material.
@@ -740,6 +854,31 @@ impl Printable for String {
740
854
741
855
> Note: The notation ` &self ` is a shorthand for ` self: &Self ` .
742
856
857
+ [ IDENTIFIER ] : identifiers.html
858
+ [ _ArrayType_ ] : #array-and-slice-types
859
+ [ _BareFunctionType_ ] : #function-pointer-types
860
+ [ _Expression_ ] : expressions.html
861
+ [ _ForLifetimes_ ] : items/generics.html#where-clauses
862
+ [ _FunctionFront_ ] : items/functions.html
863
+ [ _FunctionParametersMaybeNamed_ ] : items/functions.html
864
+ [ _ImplTraitTypeOneBound_ ] : #impl-trait
865
+ [ _ImplTraitType_ ] : #impl-trait
866
+ [ _InferredType_ ] : #inferred-type
867
+ [ _Lifetime_ ] : trait-bounds.html
868
+ [ _NeverType_ ] : #never-type
869
+ [ _ParenthesizedType_ ] : #parenthesized-types
870
+ [ _QualifiedPathInType_ ] : paths.html#qualified-paths
871
+ [ _RawPointerType_ ] : #raw-pointers-const-and-mut
872
+ [ _ReferenceType_ ] : #shared-references-
873
+ [ _SliceType_ ] : #array-and-slice-types
874
+ [ _TraitBound_ ] : trait-bounds.html
875
+ [ _TraitObjectTypeOneBound_ ] : #trait-objects
876
+ [ _TraitObjectType_ ] : #trait-objects
877
+ [ _TupleType_ ] : #tuple-types
878
+ [ _TypeNoBounds_ ] : #types
879
+ [ _TypeParamBounds_ ] : trait-bounds.html
880
+ [ _TypePath_ ] : paths.html#paths-in-types
881
+ [ _Type_ ] : #types
743
882
[ `Fn` ] : ../std/ops/trait.Fn.html
744
883
[ `FnMut` ] : ../std/ops/trait.FnMut.html
745
884
[ `FnOnce` ] : ../std/ops/trait.FnOnce.html
@@ -748,6 +887,7 @@ impl Printable for String {
748
887
[ `Send` ] : special-types-and-traits.html#send
749
888
[ `Sync` ] : special-types-and-traits.html#sync
750
889
[ `Sized` ] : special-types-and-traits.html#sized
890
+ [ `extern` ] : items/external-blocks.html
751
891
[ derive ] : attributes.html#derive
752
892
[ `Vec<T>` ] : ../std/vec/struct.Vec.html
753
893
[ dynamically sized type ] : dynamically-sized-types.html
@@ -759,3 +899,4 @@ impl Printable for String {
759
899
[ issue 47010 ] : https://github.com/rust-lang/rust/issues/47010
760
900
[ issue 33140 ] : https://github.com/rust-lang/rust/issues/33140
761
901
[ supertraits ] : items/traits.html#supertraits
902
+ [ type boundaries ] : trait-bounds.html
0 commit comments