@@ -149,9 +149,11 @@ sequence (`/**`), are interpreted as a special syntax for `doc`
149
149
` #[doc="..."] ` around the body of the comment (this includes the comment
150
150
characters themselves, i.e. ` /// Foo ` turns into ` #[doc="/// Foo"] ` ).
151
151
152
- ` //! ` comments apply to the parent of the comment, rather than the item that
153
- follows. ` //! ` comments are usually used to display information on the crate
154
- index page.
152
+ Line comments beginning with ` //! ` and block comments beginning with ` /*! ` are
153
+ doc comments that apply to the parent of the comment, rather than the item
154
+ that follows. That is, they are equivalent to writing ` #![doc="..."] ` around
155
+ the body of the comment. ` //! ` comments are usually used to display
156
+ information on the crate index page.
155
157
156
158
Non-doc comments are interpreted as a form of whitespace.
157
159
@@ -196,10 +198,11 @@ grammar as double-quoted strings. Other tokens have exact rules given.
196
198
| fn | for | if | impl | in |
197
199
| let | loop | macro | match | mod |
198
200
| move | mut | offsetof | override | priv |
199
- | pub | pure | ref | return | sizeof |
200
- | static | self | struct | super | true |
201
- | trait | type | typeof | unsafe | unsized |
202
- | use | virtual | where | while | yield |
201
+ | proc | pub | pure | ref | return |
202
+ | Self | self | sizeof | static | struct |
203
+ | super | trait | true | type | typeof |
204
+ | unsafe | unsized | use | virtual | where |
205
+ | while | yield | | | |
203
206
204
207
205
208
Each of these keywords has special meaning in its grammar, and all of them are
@@ -1099,40 +1102,31 @@ signature. Each type parameter must be explicitly declared, in an
1099
1102
angle-bracket-enclosed, comma-separated list following the function name.
1100
1103
1101
1104
``` {.ignore}
1102
- fn iter<T>(seq: &[T], f: |T| ) {
1103
- for elt in seq.iter() { f(elt); }
1105
+ fn iter<T, F >(seq: &[T], f: F) where T: Copy, F: Fn(T ) {
1106
+ for elt in seq { f(* elt); }
1104
1107
}
1105
- fn map<T, U>(seq: &[T], f: |T| -> U ) -> Vec<U> {
1108
+ fn map<T, U, F >(seq: &[T], f: F) -> Vec<U> where T: Copy, U: Copy, F: Fn(T ) -> U {
1106
1109
let mut acc = vec![];
1107
- for elt in seq.iter() { acc.push(f(elt)); }
1110
+ for elt in seq { acc.push(f(* elt)); }
1108
1111
acc
1109
1112
}
1110
1113
```
1111
1114
1112
1115
Inside the function signature and body, the name of the type parameter can be
1113
- used as a type name.
1116
+ used as a type name. [ Trait] ( #traits ) bounds can be specified for type parameters
1117
+ to allow methods with that trait to be called on values of that type. This is
1118
+ specified using the ` where ` syntax, as in the above example.
1114
1119
1115
1120
When a generic function is referenced, its type is instantiated based on the
1116
1121
context of the reference. For example, calling the ` iter ` function defined
1117
1122
above on ` [1, 2] ` will instantiate type parameter ` T ` with ` i32 ` , and require
1118
- the closure parameter to have type ` fn (i32)` .
1123
+ the closure parameter to have type ` Fn (i32)` .
1119
1124
1120
1125
The type parameters can also be explicitly supplied in a trailing
1121
1126
[ path] ( #paths ) component after the function name. This might be necessary if
1122
1127
there is not sufficient context to determine the type parameters. For example,
1123
1128
` mem::size_of::<u32>() == 4 ` .
1124
1129
1125
- Since a parameter type is opaque to the generic function, the set of operations
1126
- that can be performed on it is limited. Values of parameter type can only be
1127
- moved, not copied.
1128
-
1129
- ```
1130
- fn id<T>(x: T) -> T { x }
1131
- ```
1132
-
1133
- Similarly, [ trait] ( #traits ) bounds can be specified for type parameters to
1134
- allow methods with that trait to be called on values of that type.
1135
-
1136
1130
#### Unsafety
1137
1131
1138
1132
Unsafe operations are those that potentially violate the memory-safety
@@ -1555,7 +1549,7 @@ fn draw_twice<T: Shape>(surface: Surface, sh: T) {
1555
1549
}
1556
1550
```
1557
1551
1558
- Traits also define an [ object type ] ( #object-types ) with the same name as the
1552
+ Traits also define an [ trait object ] ( #trait-objects ) with the same name as the
1559
1553
trait. Values of this type are created by [ casting] ( #type-cast-expressions )
1560
1554
pointer values (pointing to a type for which an implementation of the given
1561
1555
trait is in scope) to pointers to the trait name, used as a type.
@@ -2146,7 +2140,7 @@ The following configurations must be defined by the implementation:
2146
2140
` "unix" ` or ` "windows" ` . The value of this configuration option is defined
2147
2141
as a configuration itself, like ` unix ` or ` windows ` .
2148
2142
* ` target_os = "..." ` . Operating system of the target, examples include
2149
- ` "win32 " ` , ` "macos" ` , ` "linux" ` , ` "android" ` , ` "freebsd" ` , ` "dragonfly" ` ,
2143
+ ` "windows " ` , ` "macos" ` , ` "ios "` , ` "linux" ` , ` "android" ` , ` "freebsd" ` , ` "dragonfly" ` ,
2150
2144
` "bitrig" ` or ` "openbsd" ` .
2151
2145
* ` target_pointer_width = "..." ` . Target pointer width in bits. This is set
2152
2146
to ` "32" ` for targets with 32-bit pointers, and likewise set to ` "64" ` for
@@ -2744,7 +2738,7 @@ A _method call_ consists of an expression followed by a single dot, an
2744
2738
identifier, and a parenthesized expression-list. Method calls are resolved to
2745
2739
methods on specific traits, either statically dispatching to a method if the
2746
2740
exact ` self ` -type of the left-hand-side is known, or dynamically dispatching if
2747
- the left-hand-side expression is an indirect [ object type ] ( #object-types ) .
2741
+ the left-hand-side expression is an indirect [ trait object ] ( #trait-objects ) .
2748
2742
2749
2743
### Field expressions
2750
2744
@@ -2812,6 +2806,33 @@ _panicked state_.
2812
2806
(["a", "b"])[10]; // panics
2813
2807
```
2814
2808
2809
+ ### Range expressions
2810
+
2811
+ ``` {.ebnf .gram}
2812
+ range_expr : expr ".." expr |
2813
+ expr ".." |
2814
+ ".." expr |
2815
+ ".." ;
2816
+ ```
2817
+
2818
+ The ` .. ` operator will construct an object of one of the ` std::ops::Range ` variants.
2819
+
2820
+ ```
2821
+ 1..2; // std::ops::Range
2822
+ 3..; // std::ops::RangeFrom
2823
+ ..4; // std::ops::RangeTo
2824
+ ..; // std::ops::RangeFull
2825
+ ```
2826
+
2827
+ The following expressions are equivalent.
2828
+
2829
+ ```
2830
+ let x = std::ops::Range {start: 0, end: 10};
2831
+ let y = 0..10;
2832
+
2833
+ assert_eq!(x,y);
2834
+ ```
2835
+
2815
2836
### Unary operator expressions
2816
2837
2817
2838
Rust defines three unary operators. They are all written as prefix operators,
@@ -3078,28 +3099,6 @@ fn ten_times<F>(f: F) where F: Fn(i32) {
3078
3099
ten_times(|j| println!("hello, {}", j));
3079
3100
```
3080
3101
3081
- ### While loops
3082
-
3083
- ``` {.ebnf .gram}
3084
- while_expr : [ lifetime ':' ] "while" no_struct_literal_expr '{' block '}' ;
3085
- ```
3086
-
3087
- A ` while ` loop begins by evaluating the boolean loop conditional expression.
3088
- If the loop conditional expression evaluates to ` true ` , the loop body block
3089
- executes and control returns to the loop conditional expression. If the loop
3090
- conditional expression evaluates to ` false ` , the ` while ` expression completes.
3091
-
3092
- An example:
3093
-
3094
- ```
3095
- let mut i = 0;
3096
-
3097
- while i < 10 {
3098
- println!("hello");
3099
- i = i + 1;
3100
- }
3101
- ```
3102
-
3103
3102
### Infinite loops
3104
3103
3105
3104
A ` loop ` expression denotes an infinite loop.
@@ -3108,10 +3107,11 @@ A `loop` expression denotes an infinite loop.
3108
3107
loop_expr : [ lifetime ':' ] "loop" '{' block '}';
3109
3108
```
3110
3109
3111
- A ` loop ` expression may optionally have a _ label_ . If a label is present, then
3112
- labeled ` break ` and ` continue ` expressions nested within this loop may exit out
3113
- of this loop or return control to its head. See [ Break
3114
- expressions] ( #break-expressions ) and [ Continue
3110
+ A ` loop ` expression may optionally have a _ label_ . The label is written as
3111
+ a lifetime preceding the loop expression, as in ` 'foo: loop{ } ` . If a
3112
+ label is present, then labeled ` break ` and ` continue ` expressions nested
3113
+ within this loop may exit out of this loop or return control to its head.
3114
+ See [ Break expressions] ( #break-expressions ) and [ Continue
3115
3115
expressions] ( #continue-expressions ) .
3116
3116
3117
3117
### Break expressions
@@ -3123,7 +3123,7 @@ break_expr : "break" [ lifetime ];
3123
3123
A ` break ` expression has an optional _ label_ . If the label is absent, then
3124
3124
executing a ` break ` expression immediately terminates the innermost loop
3125
3125
enclosing it. It is only permitted in the body of a loop. If the label is
3126
- present, then ` break foo ` terminates the loop with label ` foo ` , which need not
3126
+ present, then ` break ' foo ` terminates the loop with label ` ' foo` , which need not
3127
3127
be the innermost label enclosing the ` break ` expression, but must enclose it.
3128
3128
3129
3129
### Continue expressions
@@ -3137,12 +3137,39 @@ executing a `continue` expression immediately terminates the current iteration
3137
3137
of the innermost loop enclosing it, returning control to the loop * head* . In
3138
3138
the case of a ` while ` loop, the head is the conditional expression controlling
3139
3139
the loop. In the case of a ` for ` loop, the head is the call-expression
3140
- controlling the loop. If the label is present, then ` continue foo ` returns
3141
- control to the head of the loop with label ` foo ` , which need not be the
3140
+ controlling the loop. If the label is present, then ` continue ' foo ` returns
3141
+ control to the head of the loop with label ` ' foo` , which need not be the
3142
3142
innermost label enclosing the ` break ` expression, but must enclose it.
3143
3143
3144
3144
A ` continue ` expression is only permitted in the body of a loop.
3145
3145
3146
+ ### While loops
3147
+
3148
+ ``` {.ebnf .gram}
3149
+ while_expr : [ lifetime ':' ] "while" no_struct_literal_expr '{' block '}' ;
3150
+ ```
3151
+
3152
+ A ` while ` loop begins by evaluating the boolean loop conditional expression.
3153
+ If the loop conditional expression evaluates to ` true ` , the loop body block
3154
+ executes and control returns to the loop conditional expression. If the loop
3155
+ conditional expression evaluates to ` false ` , the ` while ` expression completes.
3156
+
3157
+ An example:
3158
+
3159
+ ```
3160
+ let mut i = 0;
3161
+
3162
+ while i < 10 {
3163
+ println!("hello");
3164
+ i = i + 1;
3165
+ }
3166
+ ```
3167
+
3168
+ Like ` loop ` expressions, ` while ` loops can be controlled with ` break ` or
3169
+ ` continue ` , and may optionally have a _ label_ . See [ infinite
3170
+ loops] ( #infinite-loops ) , [ break expressions] ( #break-expressions ) , and
3171
+ [ continue expressions] ( #continue-expressions ) for more information.
3172
+
3146
3173
### For expressions
3147
3174
3148
3175
``` {.ebnf .gram}
@@ -3177,6 +3204,11 @@ for i in 0..256 {
3177
3204
}
3178
3205
```
3179
3206
3207
+ Like ` loop ` expressions, ` for ` loops can be controlled with ` break ` or
3208
+ ` continue ` , and may optionally have a _ label_ . See [ infinite
3209
+ loops] ( #infinite-loops ) , [ break expressions] ( #break-expressions ) , and
3210
+ [ continue expressions] ( #continue-expressions ) for more information.
3211
+
3180
3212
### If expressions
3181
3213
3182
3214
``` {.ebnf .gram}
@@ -3649,23 +3681,23 @@ call_closure(closure_no_args, closure_args);
3649
3681
3650
3682
```
3651
3683
3652
- ### Object types
3684
+ ### Trait objects
3653
3685
3654
3686
Every trait item (see [ traits] ( #traits ) ) defines a type with the same name as
3655
- the trait. This type is called the _ object type _ of the trait. Object types
3687
+ the trait. This type is called the _ trait object _ of the trait. Trait objects
3656
3688
permit "late binding" of methods, dispatched using _ virtual method tables_
3657
3689
("vtables"). Whereas most calls to trait methods are "early bound" (statically
3658
3690
resolved) to specific implementations at compile time, a call to a method on an
3659
- object type is only resolved to a vtable entry at compile time. The actual
3691
+ trait objects is only resolved to a vtable entry at compile time. The actual
3660
3692
implementation for each vtable entry can vary on an object-by-object basis.
3661
3693
3662
3694
Given a pointer-typed expression ` E ` of type ` &T ` or ` Box<T> ` , where ` T `
3663
3695
implements trait ` R ` , casting ` E ` to the corresponding pointer type ` &R ` or
3664
- ` Box<R> ` results in a value of the _ object type _ ` R ` . This result is
3696
+ ` Box<R> ` results in a value of the _ trait object _ ` R ` . This result is
3665
3697
represented as a pair of pointers: the vtable pointer for the ` T `
3666
3698
implementation of ` R ` , and the pointer value of ` E ` .
3667
3699
3668
- An example of an object type :
3700
+ An example of a trait object :
3669
3701
3670
3702
```
3671
3703
trait Printable {
@@ -3685,7 +3717,7 @@ fn main() {
3685
3717
}
3686
3718
```
3687
3719
3688
- In this example, the trait ` Printable ` occurs as an object type in both the
3720
+ In this example, the trait ` Printable ` occurs as a trait object in both the
3689
3721
type signature of ` print ` , and the cast expression in ` main ` .
3690
3722
3691
3723
### Type parameters
0 commit comments