2
2
3
3
> ** <sup >Syntax</sup >** \
4
4
> _ Type_ :\
5
- >   ;  ;   ;  ; _ TypeCommon_ \
6
- >   ;  ; | _ ParenthesizedType_   ; (` + ` [ _ TypeParamBounds_ ] )<sup >?</sup >\
7
- >   ;  ; | [ _ TypePath_ ]   ; (` + ` [ _ TypeParamBounds_ ] )<sup >?</sup >\
5
+ >   ;  ;   ;  ; _ TypeNoBounds_ \
8
6
>   ;  ; | [ _ ImplTraitType_ ] \
9
7
>   ;  ; | [ _ TraitObjectType_ ]
10
8
>
11
9
> _ TypeNoBounds_ :\
12
- >   ;  ;   ;  ; _ TypeCommon_ \
13
- >   ;  ; | _ ParenthesizedType_ \
14
- >   ;  ; | [ _ TypePath_ ]
15
- >
16
- > _ TypeCommon_ :\
17
- >   ;  ;   ;  ; [ _ TupleType_ ] \
10
+ >   ;  ;   ;  ; [ _ ParenthesizedType_ ] \
11
+ >   ;  ; | [ _ ImplTraitTypeOneBound_ ] \
12
+ >   ;  ; | [ _ TraitObjectTypeOneBound_ ] \
13
+ >   ;  ; | [ _ TypePath_ ] \
14
+ >   ;  ; | [ _ TupleType_ ] \
18
15
>   ;  ; | [ _ NeverType_ ] \
19
16
>   ;  ; | [ _ RawPointerType_ ] \
20
17
>   ;  ; | [ _ ReferenceType_ ] \
23
20
>   ;  ; | [ _ InferredType_ ] \
24
21
>   ;  ; | [ _ QualifiedPathInType_ ] \
25
22
>   ;  ; | [ _ BareFunctionType_ ]
26
- >
27
- > _ ParenthesizedType_ :\
28
- >   ;  ;   ;  ; ` ( ` _ Type_ ` ) `
29
23
30
24
Every variable, item and value in a Rust program has a type. The _ type_ of a
31
25
* value* defines the interpretation of the memory holding it.
@@ -146,8 +140,7 @@ any other type.
146
140
> ** <sup >Syntax</sup >** \
147
141
> _ TupleType_ :\
148
142
>   ;  ;   ;  ; ` ( ` ` ) ` \
149
- >   ;  ; | ` ( ` [ _ Type_ ] ` , ` ` ) ` \
150
- >   ;  ; | ` ( ` [ _ Type_ ]   ; ( ` , ` [ _ Type_ ] ) <sup >+</sup > ` , ` <sup >?</sup > ` ) `
143
+ >   ;  ; | ` ( ` ( [ _ Type_ ] ` , ` )<sup >+</sup > [ _ Type_ ] <sup >?</sup > ` ) `
151
144
152
145
A tuple * type* is a heterogeneous product of other types, called the * elements*
153
146
of the tuple. It has no nominal name and is instead structurally typed.
@@ -175,6 +168,24 @@ assert_eq!(p.1, "ten");
175
168
For historical reasons and convenience, the tuple type with no elements (` () ` )
176
169
is often called ‘unit’ or ‘the unit type’.
177
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
+
178
189
## Array, and Slice types
179
190
180
191
> ** <sup >Syntax</sup >** \
@@ -636,6 +647,9 @@ Because captures are often by reference, the following general rules arise:
636
647
> ** <sup >Syntax</sup >** \
637
648
> _ TraitObjectType_ :\
638
649
>   ;  ; ` dyn ` <sup >?</sup > [ _ TypeParamBounds_ ]
650
+ >
651
+ > _ TraitObjectTypeOneBound_ :\
652
+ >   ;  ; ` dyn ` <sup >?</sup > [ _ TraitBound_ ]
639
653
640
654
A * trait object* is an opaque value of another type that implements a set of
641
655
traits. The set of traits is made up of an [ object safe] * base trait* plus any
@@ -748,7 +762,7 @@ let x: Vec<_> = (0..10).collect();
748
762
<!--
749
763
What else should be said here?
750
764
The only documentation I am aware of is https://rust-lang-nursery.github.io/rustc-guide/type-inference.html
751
- Should there be a broader discussion of type inference somewhere?
765
+ There should be a broader discussion of type inference somewhere.
752
766
-->
753
767
754
768
## Type parameters
@@ -775,6 +789,8 @@ Here, `first` has type `A`, referring to `to_vec`'s `A` type parameter; and
775
789
776
790
> ** <sup >Syntax</sup >** \
777
791
> _ ImplTraitType_ : ` impl ` [ _ TypeParamBounds_ ]
792
+ >
793
+ > _ ImplTraitTypeOneBound_ : ` impl ` [ _ TraitBound_ ]
778
794
779
795
### Anonymous type parameters
780
796
@@ -845,15 +861,18 @@ impl Printable for String {
845
861
[ _ForLifetimes_ ] : items/generics.html#where-clauses
846
862
[ _FunctionFront_ ] : items/functions.html
847
863
[ _FunctionParametersMaybeNamed_ ] : items/functions.html
864
+ [ _ImplTraitTypeOneBound_ ] : #impl-trait
848
865
[ _ImplTraitType_ ] : #impl-trait
849
866
[ _InferredType_ ] : #inferred-type
850
867
[ _Lifetime_ ] : trait-bounds.html
851
868
[ _NeverType_ ] : #never-type
852
- [ _ParenthesizedType_ ] : #parenthesized-type
869
+ [ _ParenthesizedType_ ] : #parenthesized-types
853
870
[ _QualifiedPathInType_ ] : paths.html#qualified-paths
854
871
[ _RawPointerType_ ] : #raw-pointers-const-and-mut
855
872
[ _ReferenceType_ ] : #shared-references-
856
873
[ _SliceType_ ] : #array-and-slice-types
874
+ [ _TraitBound_ ] : trait-bounds.html
875
+ [ _TraitObjectTypeOneBound_ ] : #trait-objects
857
876
[ _TraitObjectType_ ] : #trait-objects
858
877
[ _TupleType_ ] : #tuple-types
859
878
[ _TypeNoBounds_ ] : #types
@@ -880,3 +899,4 @@ impl Printable for String {
880
899
[ issue 47010 ] : https://github.com/rust-lang/rust/issues/47010
881
900
[ issue 33140 ] : https://github.com/rust-lang/rust/issues/33140
882
901
[ supertraits ] : items/traits.html#supertraits
902
+ [ type boundaries ] : trait-bounds.html
0 commit comments