Skip to content

Commit 95fe7d2

Browse files
committed
Grammar: types
1 parent 49d6c3c commit 95fe7d2

File tree

2 files changed

+113
-6
lines changed

2 files changed

+113
-6
lines changed

src/tokens.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ usages and meanings are defined in the linked pages.
548548
| `>=` | Ge | [Greater than or equal to][comparison], [Generics]
549549
| `<=` | Le | [Less than or equal to][comparison]
550550
| `@` | At | [Subpattern binding]
551-
| `_` | Underscore | [Wildcard patterns], Inferred types
551+
| `_` | Underscore | [Wildcard patterns], [Inferred types]
552552
| `.` | Dot | [Field access][field], [Tuple index]
553553
| `..` | DotDot | [Range][range], [Struct expressions], [Patterns]
554554
| `...` | DotDotDot | [Variadic functions][extern], [Range patterns]
@@ -576,6 +576,7 @@ them are referred to as "token trees" in [macros]. The three types of brackets
576576
| `(` `)` | Parentheses |
577577

578578

579+
[Inferred types]: types.html#inferred-type
579580
[Operator expressions]: expressions/operator-expr.html
580581
[Range patterns]: patterns.html#range-patterns
581582
[Reference patterns]: patterns.html#reference-patterns

src/types.md

Lines changed: 111 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,32 @@
11
# Types
22

3+
> **<sup>Syntax</sup>**\
4+
> _Type_ :\
5+
> &nbsp;&nbsp; &nbsp;&nbsp; _TypeCommon_\
6+
> &nbsp;&nbsp; | _ParenthesizedType_&nbsp;(`+` [_TypeParamBounds_])<sup>?</sup>\
7+
> &nbsp;&nbsp; | [_TypePath_]&nbsp;(`+` [_TypeParamBounds_])<sup>?</sup>\
8+
> &nbsp;&nbsp; | [_ImplTraitType_]\
9+
> &nbsp;&nbsp; | [_TraitObjectType_]
10+
>
11+
> _TypeNoBounds_ :\
12+
> &nbsp;&nbsp; &nbsp;&nbsp; _TypeCommon_\
13+
> &nbsp;&nbsp; | _ParenthesizedType_\
14+
> &nbsp;&nbsp; | [_TypePath_]
15+
>
16+
> _TypeCommon_ :\
17+
> &nbsp;&nbsp; &nbsp;&nbsp; [_TupleType_]\
18+
> &nbsp;&nbsp; | [_NeverType_]\
19+
> &nbsp;&nbsp; | [_RawPointerType_]\
20+
> &nbsp;&nbsp; | [_ReferenceType_]\
21+
> &nbsp;&nbsp; | [_ArrayType_]\
22+
> &nbsp;&nbsp; | [_SliceType_]\
23+
> &nbsp;&nbsp; | [_InferredType_]\
24+
> &nbsp;&nbsp; | [_QualifiedPathInType_]\
25+
> &nbsp;&nbsp; | [_BareFunctionType_]
26+
>
27+
> _ParenthesizedType_ :\
28+
> &nbsp;&nbsp; &nbsp;&nbsp; `(` _Type_ `)`
29+
330
Every variable, item and value in a Rust program has a type. The _type_ of a
431
*value* defines the interpretation of the memory holding it.
532

@@ -107,12 +134,21 @@ instantiated through a pointer type, such as `&str`.
107134

108135
## Never type
109136

137+
> **<sup>Syntax</sup>**\
138+
> _NeverType_ : `!`
139+
110140
The never type `!` is a type with no values, representing the result of
111141
computations that never complete. Expressions of type `!` can be coerced into
112142
any other type.
113143

114144
## Tuple types
115145

146+
> **<sup>Syntax</sup>**\
147+
> _TupleType_ :\
148+
> &nbsp;&nbsp; &nbsp;&nbsp; `(` `)`\
149+
> &nbsp;&nbsp; | `(` [_Type_] `,` `)`\
150+
> &nbsp;&nbsp; | `(` [_Type_]&nbsp;( `,` [_Type_] ) <sup>+</sup> `,`<sup>?</sup> `)`
151+
116152
A tuple *type* is a heterogeneous product of other types, called the *elements*
117153
of the tuple. It has no nominal name and is instead structurally typed.
118154

@@ -141,13 +177,21 @@ is often called ‘unit’ or ‘the unit type’.
141177

142178
## Array, and Slice types
143179

144-
Rust has two different types for a list of items:
180+
> **<sup>Syntax</sup>**\
181+
> _ArrayType_ :\
182+
> &nbsp;&nbsp; `[` [_Type_] `;` [_Expression_] `]`
183+
>
184+
> _SliceType_ :\
185+
> &nbsp;&nbsp; `[` [_Type_] `]`
186+
187+
Rust has two different types for a list of items of the same type:
145188

146189
* `[T; N]`, an 'array'
147190
* `[T]`, a 'slice'
148191

149192
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).
151195

152196
A slice is a [dynamically sized type] representing a 'view' into an array. To
153197
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.
276320

277321
### Shared references (`&`)
278322

323+
> **<sup>Syntax</sup>**\
324+
> _ReferenceType_ :\
325+
> &nbsp;&nbsp; `&` [_Lifetime_]<sup>?</sup> `mut`<sup>?</sup> [_TypeNoBounds_]
326+
279327
These point to memory _owned by some other value_. When a shared reference to a
280328
value is created it prevents direct mutation of the value. [Interior
281329
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`.
295343

296344
### Raw pointers (`*const` and `*mut`)
297345

346+
> **<sup>Syntax</sup>**\
347+
> _RawPointerType_ :\
348+
> &nbsp;&nbsp; `*` ( `mut` | `const` ) [_TypeNoBounds_]
349+
298350
Raw pointers are pointers without safety or liveness guarantees. Raw pointers
299351
are written as `*const T` or `*mut T`, for example `*const i32` means a raw
300352
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`],
366418

367419
## Function pointer types
368420

421+
> **<sup>Syntax</sup>**\
422+
> _BareFunctionType_ :\
423+
> &nbsp;&nbsp; [_ForLifetimes_]<sup>?</sup> [_FunctionFront_] `fn`\
424+
> &nbsp;&nbsp; &nbsp;&nbsp; `(` [_FunctionParametersMaybeNamedVariadic_]<sup>?</sup> `)` _BareFunctionReturnType_<sup>?</sup>
425+
>
426+
> _BareFunctionReturnType_:\
427+
> &nbsp;&nbsp; `->` [_TypeNoBounds_]
428+
429+
<!-- TODO: _FunctionParametersMaybeNamedVariadic_ -->
430+
369431
Function pointer types, written using the `fn` keyword, refer to a function
370432
whose identity is not necessarily known at compile-time. They can be created
371433
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:
560622

561623
> **<sup>Syntax</sup>**\
562624
> _TraitObjectType_ :\
563-
> &nbsp;&nbsp; `dyn`<sup>?</sup> _TypeParamBounds_
625+
> &nbsp;&nbsp; `dyn`<sup>?</sup> [_TypeParamBounds_]
564626
565627
A *trait object* is an opaque value of another type that implements a set of
566628
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.
658720

659721
[defaults]: lifetime-elision.html#default-trait-object-lifetimes
660722

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+
661741
## Type parameters
662742

663743
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> {
678758
Here, `first` has type `A`, referring to `to_vec`'s `A` type parameter; and
679759
`rest` has type `Vec<A>`, a vector with element type `A`.
680760

681-
## Anonymous type parameters
761+
## Impl trait
762+
763+
> **<sup>Syntax</sup>**\
764+
> _ImplTraitType_ : `impl` [_TypeParamBounds_]
765+
766+
### Anonymous type parameters
682767

683768
> Note: This section is a placeholder for more comprehensive reference
684769
> material.
@@ -692,7 +777,7 @@ bounds of the anonymous type parameter.
692777

693778
They are written as `impl` followed by a set of trait bounds.
694779

695-
## Abstract return types
780+
### Abstract return types
696781

697782
> Note: This section is a placeholder for more comprehensive reference
698783
> material.
@@ -740,6 +825,27 @@ impl Printable for String {
740825

741826
> Note: The notation `&self` is a shorthand for `self: &Self`.
742827
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
743849
[`Fn`]: ../std/ops/trait.Fn.html
744850
[`FnMut`]: ../std/ops/trait.FnMut.html
745851
[`FnOnce`]: ../std/ops/trait.FnOnce.html

0 commit comments

Comments
 (0)