Skip to content

Commit 4c862cc

Browse files
committed
Address review comments.
1 parent 0180414 commit 4c862cc

File tree

1 file changed

+36
-16
lines changed

1 file changed

+36
-16
lines changed

src/types.md

+36-16
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,16 @@
22

33
> **<sup>Syntax</sup>**\
44
> _Type_ :\
5-
> &nbsp;&nbsp; &nbsp;&nbsp; _TypeCommon_\
6-
> &nbsp;&nbsp; | _ParenthesizedType_&nbsp;(`+` [_TypeParamBounds_])<sup>?</sup>\
7-
> &nbsp;&nbsp; | [_TypePath_]&nbsp;(`+` [_TypeParamBounds_])<sup>?</sup>\
5+
> &nbsp;&nbsp; &nbsp;&nbsp; _TypeNoBounds_\
86
> &nbsp;&nbsp; | [_ImplTraitType_]\
97
> &nbsp;&nbsp; | [_TraitObjectType_]
108
>
119
> _TypeNoBounds_ :\
12-
> &nbsp;&nbsp; &nbsp;&nbsp; _TypeCommon_\
13-
> &nbsp;&nbsp; | _ParenthesizedType_\
14-
> &nbsp;&nbsp; | [_TypePath_]
15-
>
16-
> _TypeCommon_ :\
17-
> &nbsp;&nbsp; &nbsp;&nbsp; [_TupleType_]\
10+
> &nbsp;&nbsp; &nbsp;&nbsp; [_ParenthesizedType_]\
11+
> &nbsp;&nbsp; | [_ImplTraitTypeOneBound_]\
12+
> &nbsp;&nbsp; | [_TraitObjectTypeOneBound_]\
13+
> &nbsp;&nbsp; | [_TypePath_]\
14+
> &nbsp;&nbsp; | [_TupleType_]\
1815
> &nbsp;&nbsp; | [_NeverType_]\
1916
> &nbsp;&nbsp; | [_RawPointerType_]\
2017
> &nbsp;&nbsp; | [_ReferenceType_]\
@@ -23,9 +20,6 @@
2320
> &nbsp;&nbsp; | [_InferredType_]\
2421
> &nbsp;&nbsp; | [_QualifiedPathInType_]\
2522
> &nbsp;&nbsp; | [_BareFunctionType_]
26-
>
27-
> _ParenthesizedType_ :\
28-
> &nbsp;&nbsp; &nbsp;&nbsp; `(` _Type_ `)`
2923
3024
Every variable, item and value in a Rust program has a type. The _type_ of a
3125
*value* defines the interpretation of the memory holding it.
@@ -146,8 +140,7 @@ any other type.
146140
> **<sup>Syntax</sup>**\
147141
> _TupleType_ :\
148142
> &nbsp;&nbsp; &nbsp;&nbsp; `(` `)`\
149-
> &nbsp;&nbsp; | `(` [_Type_] `,` `)`\
150-
> &nbsp;&nbsp; | `(` [_Type_]&nbsp;( `,` [_Type_] ) <sup>+</sup> `,`<sup>?</sup> `)`
143+
> &nbsp;&nbsp; | `(` ( [_Type_] `,` )<sup>+</sup> [_Type_]<sup>?</sup> `)`
151144
152145
A tuple *type* is a heterogeneous product of other types, called the *elements*
153146
of the tuple. It has no nominal name and is instead structurally typed.
@@ -175,6 +168,24 @@ assert_eq!(p.1, "ten");
175168
For historical reasons and convenience, the tuple type with no elements (`()`)
176169
is often called ‘unit’ or ‘the unit type’.
177170

171+
## Parenthesized types
172+
173+
> _ParenthesizedType_ :\
174+
> &nbsp;&nbsp; `(` [_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+
178189
## Array, and Slice types
179190

180191
> **<sup>Syntax</sup>**\
@@ -636,6 +647,9 @@ Because captures are often by reference, the following general rules arise:
636647
> **<sup>Syntax</sup>**\
637648
> _TraitObjectType_ :\
638649
> &nbsp;&nbsp; `dyn`<sup>?</sup> [_TypeParamBounds_]
650+
>
651+
> _TraitObjectTypeOneBound_ :\
652+
> &nbsp;&nbsp; `dyn`<sup>?</sup> [_TraitBound_]
639653
640654
A *trait object* is an opaque value of another type that implements a set of
641655
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();
748762
<!--
749763
What else should be said here?
750764
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.
752766
-->
753767

754768
## Type parameters
@@ -775,6 +789,8 @@ Here, `first` has type `A`, referring to `to_vec`'s `A` type parameter; and
775789

776790
> **<sup>Syntax</sup>**\
777791
> _ImplTraitType_ : `impl` [_TypeParamBounds_]
792+
>
793+
> _ImplTraitTypeOneBound_ : `impl` [_TraitBound_]
778794
779795
### Anonymous type parameters
780796

@@ -845,15 +861,18 @@ impl Printable for String {
845861
[_ForLifetimes_]: items/generics.html#where-clauses
846862
[_FunctionFront_]: items/functions.html
847863
[_FunctionParametersMaybeNamed_]: items/functions.html
864+
[_ImplTraitTypeOneBound_]: #impl-trait
848865
[_ImplTraitType_]: #impl-trait
849866
[_InferredType_]: #inferred-type
850867
[_Lifetime_]: trait-bounds.html
851868
[_NeverType_]: #never-type
852-
[_ParenthesizedType_]: #parenthesized-type
869+
[_ParenthesizedType_]: #parenthesized-types
853870
[_QualifiedPathInType_]: paths.html#qualified-paths
854871
[_RawPointerType_]: #raw-pointers-const-and-mut
855872
[_ReferenceType_]: #shared-references-
856873
[_SliceType_]: #array-and-slice-types
874+
[_TraitBound_]: trait-bounds.html
875+
[_TraitObjectTypeOneBound_]: #trait-objects
857876
[_TraitObjectType_]: #trait-objects
858877
[_TupleType_]: #tuple-types
859878
[_TypeNoBounds_]: #types
@@ -880,3 +899,4 @@ impl Printable for String {
880899
[issue 47010]: https://github.com/rust-lang/rust/issues/47010
881900
[issue 33140]: https://github.com/rust-lang/rust/issues/33140
882901
[supertraits]: items/traits.html#supertraits
902+
[type boundaries]: trait-bounds.html

0 commit comments

Comments
 (0)