11# Types
22
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+
330Every 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+
110140The never type ` ! ` is a type with no values, representing the result of
111141computations that never complete. Expressions of type ` ! ` can be coerced into
112142any other type.
113143
114144## Tuple types
115145
146+ > ** <sup >Syntax</sup >** \
147+ > _ TupleType_ :\
148+ >   ;  ;   ;  ; ` ( ` ` ) ` \
149+ >   ;  ; | ` ( ` [ _ Type_ ] ` , ` ` ) ` \
150+ >   ;  ; | ` ( ` [ _ Type_ ]   ; ( ` , ` [ _ Type_ ] ) <sup >+</sup > ` , ` <sup >?</sup > ` ) `
151+
116152A tuple * type* is a heterogeneous product of other types, called the * elements*
117153of 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+ >   ;  ; ` [ ` [ _ Type_ ] ` ; ` [ _ Expression_ ] ` ] `
183+ >
184+ > _ SliceType_ :\
185+ >   ;  ; ` [ ` [ _ 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
149192An 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
152196A slice is a [ dynamically sized type] representing a 'view' into an array. To
153197use 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+ >   ;  ; ` & ` [ _ Lifetime_ ] <sup >?</sup > ` mut ` <sup >?</sup > [ _ TypeNoBounds_ ]
326+
279327These point to memory _ owned by some other value_ . When a shared reference to a
280328value is created it prevents direct mutation of the value. [ Interior
281329mutability] ( 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+ >   ;  ; ` * ` ( ` mut ` | ` const ` ) [ _ TypeNoBounds_ ]
349+
298350Raw pointers are pointers without safety or liveness guarantees. Raw pointers
299351are written as ` *const T ` or ` *mut T ` , for example ` *const i32 ` means a raw
300352pointer 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+ >   ;  ; [ _ ForLifetimes_ ] <sup >?</sup > [ _ FunctionFront_ ] ` fn ` \
424+ >   ;  ;   ;  ; ` ( ` [ _ FunctionParametersMaybeNamedVariadic_ ] <sup >?</sup > ` ) ` _ BareFunctionReturnType_ <sup >?</sup >
425+ >
426+ > _ BareFunctionReturnType_ :\
427+ >   ;  ; ` -> ` [ _ TypeNoBounds_ ]
428+
429+ <!-- TODO: _FunctionParametersMaybeNamedVariadic_ -->
430+
369431Function pointer types, written using the ` fn ` keyword, refer to a function
370432whose identity is not necessarily known at compile-time. They can be created
371433via 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- >   ;  ; ` dyn ` <sup >?</sup > _ TypeParamBounds_
625+ >   ;  ; ` dyn ` <sup >?</sup > [ _ TypeParamBounds_ ]
564626
565627A * trait object* is an opaque value of another type that implements a set of
566628traits. 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
663743Within 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> {
678758Here, ` 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
693778They 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