@@ -89,7 +89,7 @@ A _literal expression_ consists of one of the [literal](tokens.html#literals) fo
89
89
described earlier. It directly describes a number, character, string, boolean
90
90
value, or the unit value.
91
91
92
- ``` text
92
+ ``` rust
93
93
(); // unit type
94
94
" hello" ; // string type
95
95
'5' ; // character type
@@ -108,15 +108,15 @@ Tuples are written by enclosing zero or more comma-separated expressions in
108
108
parentheses. They are used to create [ tuple-typed] ( types.html#tuple-types )
109
109
values.
110
110
111
- ``` {.tuple}
111
+ ``` rust
112
112
(0.0 , 4.5 );
113
113
(" a" , 4usize , true );
114
114
```
115
115
116
116
You can disambiguate a single-element tuple from a value in parentheses with a
117
117
comma:
118
118
119
- ```
119
+ ``` rust
120
120
(0 ,); // single-element tuple
121
121
(0 ); // zero in parentheses
122
122
```
@@ -141,7 +141,7 @@ A _unit-like struct expression_ consists only of the [path](paths.html) of a
141
141
142
142
The following are examples of struct expressions:
143
143
144
- ```
144
+ ``` rust
145
145
# struct Point { x : f64 , y : f64 }
146
146
# struct NothingInMe { }
147
147
# struct TuplePoint (f64 , f64 );
@@ -166,7 +166,7 @@ the same type as the base expression) with the given values for the fields that
166
166
were explicitly specified and the values in the base expression for all other
167
167
fields.
168
168
169
- ```
169
+ ``` rust
170
170
# struct Point3d { x : i32 , y : i32 , z : i32 }
171
171
let base = Point3d {x : 1 , y : 2 , z : 3 };
172
172
Point3d {y : 0 , z : 10 , .. base };
@@ -180,7 +180,7 @@ This allows a compact syntax with less duplication.
180
180
181
181
Example:
182
182
183
- ```
183
+ ``` rust
184
184
# struct Point3d { x : i32 , y : i32 , z : i32 }
185
185
# let x = 0 ;
186
186
# let y_value = 0 ;
@@ -199,13 +199,13 @@ the block itself.
199
199
A block will execute each statement sequentially, and then execute the
200
200
expression (if given). If the block ends in a statement, its value is ` () ` :
201
201
202
- ```
202
+ ``` rust
203
203
let x : () = { println! (" Hello." ); };
204
204
```
205
205
206
206
If it ends in an expression, its value and type are that of the expression:
207
207
208
- ```
208
+ ``` rust
209
209
let x : i32 = { println! (" Hello." ); 5 };
210
210
211
211
assert_eq! (5 , x );
@@ -227,7 +227,7 @@ identifier, when not immediately followed by a parenthesized expression-list
227
227
(the latter is a [ method call expression] ( #method-call-expressions ) ). A field
228
228
expression denotes a field of a [ struct] ( types.html#struct-types ) .
229
229
230
- ``` {. ignore .field}
230
+ ``` rust, ignore
231
231
mystruct.myfield;
232
232
foo().x;
233
233
(Struct {a: 10, b: 20}).a;
@@ -252,7 +252,7 @@ In the `[expr ';' expr]` form, the expression after the `';'` must be a
252
252
constant expression that can be evaluated at compile time, such as a
253
253
[ literal] ( tokens.html#literals ) or a [ static item] ( items.html#static-items ) .
254
254
255
- ```
255
+ ``` rust
256
256
[1 , 2 , 3 , 4 ];
257
257
[" a" , " b" , " c" , " d" ];
258
258
[0 ; 128 ]; // array with 128 zeros
@@ -271,7 +271,7 @@ bounds-checked at compile-time for constant arrays being accessed with a
271
271
constant index value. Otherwise a check will be performed at run-time that
272
272
will put the thread in a _ panicked state_ if it fails.
273
273
274
- ``` {should-fail}
274
+ ``` rust,should_panic
275
275
([1, 2, 3, 4])[0];
276
276
277
277
let x = (["a", "b"])[10]; // compiler error: const index-expr is out of bounds
@@ -292,7 +292,7 @@ autoderefs to more.
292
292
293
293
The ` .. ` operator will construct an object of one of the ` std::ops::Range ` variants.
294
294
295
- ```
295
+ ``` rust
296
296
1 .. 2 ; // std::ops::Range
297
297
3 .. ; // std::ops::RangeFrom
298
298
.. 4 ; // std::ops::RangeTo
@@ -301,7 +301,7 @@ The `..` operator will construct an object of one of the `std::ops::Range` varia
301
301
302
302
The following expressions are equivalent.
303
303
304
- ```
304
+ ``` rust
305
305
let x = std :: ops :: Range {start : 0 , end : 10 };
306
306
let y = 0 .. 10 ;
307
307
@@ -311,15 +311,15 @@ assert_eq!(x, y);
311
311
Similarly, the ` ... ` operator will construct an object of one of the
312
312
` std::ops::RangeInclusive ` variants.
313
313
314
- ```
314
+ ``` rust
315
315
# #![feature(inclusive_range_syntax)]
316
316
1 ... 2 ; // std::ops::RangeInclusive
317
317
... 4 ; // std::ops::RangeToInclusive
318
318
```
319
319
320
320
The following expressions are equivalent.
321
321
322
- ```
322
+ ``` rust
323
323
# #![feature(inclusive_range_syntax, inclusive_range)]
324
324
let x = std :: ops :: RangeInclusive :: NonEmpty {start : 0 , end : 10 };
325
325
let y = 0 ... 10 ;
@@ -464,7 +464,7 @@ on the right-hand side.
464
464
465
465
An example of an ` as ` expression:
466
466
467
- ```
467
+ ``` rust
468
468
# fn sum (values : & [f64 ]) -> f64 { 0.0 }
469
469
# fn len (values : & [f64 ]) -> i32 { 0 }
470
470
@@ -493,7 +493,7 @@ Evaluating an assignment expression [either copies or
493
493
moves] ( #moved-and-copied-types ) its right-hand operand to its left-hand
494
494
operand.
495
495
496
- ```
496
+ ``` rust
497
497
# let mut x = 0 ;
498
498
# let y = 0 ;
499
499
x = y ;
@@ -512,7 +512,7 @@ Any such expression always has the [`unit`](types.html#tuple-types) type.
512
512
The precedence of Rust binary operators is ordered as follows, going from
513
513
strong to weak:
514
514
515
- ``` {. text .precedence}
515
+ ``` text
516
516
as :
517
517
* / %
518
518
+ -
@@ -540,7 +540,7 @@ within an expression.
540
540
541
541
An example of a parenthesized expression:
542
542
543
- ```
543
+ ``` rust
544
544
let x : i32 = (2 + 3 ) * 4 ;
545
545
```
546
546
@@ -553,7 +553,7 @@ eventually returns, then the expression completes.
553
553
554
554
Some examples of call expressions:
555
555
556
- ```
556
+ ``` rust
557
557
# fn add (x : i32 , y : i32 ) -> i32 { 0 }
558
558
559
559
let x : i32 = add (1i32 , 2i32 );
@@ -592,7 +592,7 @@ In this example, we define a function `ten_times` that takes a higher-order
592
592
function argument, and we then call it with a lambda expression as an argument,
593
593
followed by a lambda expression that moves values from its environment.
594
594
595
- ```
595
+ ``` rust
596
596
fn ten_times <F >(f : F ) where F : Fn (i32 ) {
597
597
for index in 0 .. 10 {
598
598
f (index );
@@ -646,7 +646,7 @@ conditional expression evaluates to `false`, the `while` expression completes.
646
646
647
647
An example:
648
648
649
- ```
649
+ ``` rust
650
650
let mut i = 0 ;
651
651
652
652
while i < 10 {
@@ -667,7 +667,7 @@ by an implementation of `std::iter::IntoIterator`.
667
667
668
668
An example of a ` for ` loop over the contents of an array:
669
669
670
- ```
670
+ ``` rust
671
671
# type Foo = i32 ;
672
672
# fn bar (f : & Foo ) { }
673
673
# let a = 0 ;
@@ -683,7 +683,7 @@ for e in v {
683
683
684
684
An example of a for loop over a series of integers:
685
685
686
- ```
686
+ ``` rust
687
687
# fn bar (b : usize ) { }
688
688
for i in 0 .. 256 {
689
689
bar (i );
@@ -738,7 +738,7 @@ the inside of the match.
738
738
739
739
An example of a ` match ` expression:
740
740
741
- ```
741
+ ``` rust
742
742
let x = 1 ;
743
743
744
744
match x {
@@ -759,7 +759,7 @@ bind to a reference by using the `ref` keyword, or to a mutable reference using
759
759
Subpatterns can also be bound to variables by the use of the syntax `variable @
760
760
subpattern`. For example:
761
761
762
- ```
762
+ ``` rust
763
763
let x = 1 ;
764
764
765
765
match x {
@@ -772,7 +772,7 @@ Patterns can also dereference pointers by using the `&`, `&mut` and `box`
772
772
symbols, as appropriate. For example, these two matches on ` x: &i32 ` are
773
773
equivalent:
774
774
775
- ```
775
+ ``` rust
776
776
# let x = & 3 ;
777
777
let y = match * x { 0 => " zero" , _ => " some" };
778
778
let z = match x { & 0 => " zero" , _ => " some" };
@@ -783,7 +783,7 @@ assert_eq!(y, z);
783
783
Multiple match patterns may be joined with the ` | ` operator. A range of values
784
784
may be specified with ` ... ` . For example:
785
785
786
- ```
786
+ ``` rust
787
787
# let x = 2 ;
788
788
789
789
let message = match x {
@@ -802,7 +802,7 @@ criteria for matching a case. Pattern guards appear after the pattern and
802
802
consist of a bool-typed expression following the ` if ` keyword. A pattern guard
803
803
may refer to the variables bound within the pattern they follow.
804
804
805
- ```
805
+ ``` rust
806
806
# let maybe_digit = Some (0 );
807
807
# fn process_digit (i : i32 ) { }
808
808
# fn process_other (i : i32 ) { }
@@ -822,7 +822,7 @@ pattern. If the value of the expression on the right hand side of the `let`
822
822
statement matches the pattern, the corresponding block will execute, otherwise
823
823
flow proceeds to the first ` else ` block that follows.
824
824
825
- ```
825
+ ``` rust
826
826
let dish = (" Ham" , " Eggs" );
827
827
828
828
// this body will be skipped because the pattern is refuted
@@ -853,7 +853,7 @@ transfers control to the caller frame.
853
853
854
854
An example of a ` return ` expression:
855
855
856
- ```
856
+ ``` rust
857
857
fn max (a : i32 , b : i32 ) -> i32 {
858
858
if a > b {
859
859
return a ;
0 commit comments