@@ -39,7 +39,7 @@ For example, the pattern used in:
39
39
if let
40
40
Person {
41
41
car : Some (_ ),
42
- age : person_age @ 13 ... 19 ,
42
+ age : person_age @ 13 ..= 19 ,
43
43
name : ref person_name ,
44
44
..
45
45
} = person
@@ -202,7 +202,8 @@ The wildcard pattern is always irrefutable.
202
202
203
203
> ** <sup >Syntax</sup >** \
204
204
> _ RangePattern_ :\
205
- >   ;  ; _ RangePatternBound_ ` ... ` _ RangePatternBound_
205
+ >   ;  ;  ;  ; _ RangePatternBound_ ` ..= ` _ RangePatternBound_ \
206
+ >   ;  ; | _ RangePatternBound_ ` ... ` _ RangePatternBound_
206
207
>
207
208
> _ RangePatternBound_ :\
208
209
>   ;  ;   ;  ; [ CHAR_LITERAL] \
@@ -216,11 +217,13 @@ The wildcard pattern is always irrefutable.
216
217
[ _QualifiedPathInExpression_ ] : paths.html
217
218
218
219
Range patterns match values that are within the closed range defined by its lower and
219
- upper bounds. For example, a pattern ` 'm'... 'p' ` will match only the values ` 'm' ` , ` 'n' ` ,
220
+ upper bounds. For example, a pattern ` 'm'..= 'p' ` will match only the values ` 'm' ` , ` 'n' ` ,
220
221
` 'o' ` , and ` 'p' ` . The bounds can be literals or paths that point to constant values.
221
222
222
- A pattern a ` ... ` b must always have a &le ; b. Thus, it is not possible to have a range
223
- pattern ` 10...0 ` , for example.
223
+ A pattern a ` ..= ` b must always have a &le ; b. Thus, it is not possible to have a range
224
+ pattern ` 10..=0 ` , for example.
225
+
226
+ The ` ... ` syntax is kept for backwards compatibility.
224
227
225
228
Range patterns only work on scalar types. The accepted types are:
226
229
@@ -235,17 +238,17 @@ Examples:
235
238
``` rust
236
239
# let c = 'f' ;
237
240
let valid_variable = match c {
238
- 'a' ... 'z' => true ,
239
- 'A' ... 'Z' => true ,
240
- 'α' ... 'ω' => true ,
241
+ 'a' ..= 'z' => true ,
242
+ 'A' ..= 'Z' => true ,
243
+ 'α' ..= 'ω' => true ,
241
244
_ => false ,
242
245
};
243
246
244
247
# let ph = 10 ;
245
248
println! (" {}" , match ph {
246
- 0 ... 6 => " acid" ,
249
+ 0 ..= 6 => " acid" ,
247
250
7 => " neutral" ,
248
- 8 ... 14 => " base" ,
251
+ 8 ..= 14 => " base" ,
249
252
_ => unreachable! (),
250
253
});
251
254
@@ -262,9 +265,9 @@ println!("{}", match ph {
262
265
# let altitude = 70 ;
263
266
#
264
267
println! (" {}" , match altitude {
265
- TROPOSPHERE_MIN ... TROPOSPHERE_MAX => " troposphere" ,
266
- STRATOSPHERE_MIN ... STRATOSPHERE_MAX => " stratosphere" ,
267
- MESOSPHERE_MIN ... MESOSPHERE_MAX => " mesosphere" ,
268
+ TROPOSPHERE_MIN ..= TROPOSPHERE_MAX => " troposphere" ,
269
+ STRATOSPHERE_MIN ..= STRATOSPHERE_MAX => " stratosphere" ,
270
+ MESOSPHERE_MIN ..= MESOSPHERE_MAX => " mesosphere" ,
268
271
_ => " outer space, maybe" ,
269
272
});
270
273
@@ -274,7 +277,7 @@ println!("{}", match altitude {
274
277
# }
275
278
# let n_items = 20_832_425 ;
276
279
# let bytes_per_item = 12 ;
277
- if let size @ binary :: MEGA ... binary :: GIGA = n_items * bytes_per_item {
280
+ if let size @ binary :: MEGA ..= binary :: GIGA = n_items * bytes_per_item {
278
281
println! (" It fits and occupies {} bytes" , size );
279
282
}
280
283
@@ -292,16 +295,16 @@ if let size @ binary::MEGA...binary::GIGA = n_items * bytes_per_item {
292
295
# }
293
296
// using qualified paths:
294
297
println! (" {}" , match 0xfacade {
295
- 0 ... <u8 as MaxValue >:: MAX => " fits in a u8" ,
296
- 0 ... <u16 as MaxValue >:: MAX => " fits in a u16" ,
297
- 0 ... <u32 as MaxValue >:: MAX => " fits in a u32" ,
298
+ 0 ..= <u8 as MaxValue >:: MAX => " fits in a u8" ,
299
+ 0 ..= <u16 as MaxValue >:: MAX => " fits in a u16" ,
300
+ 0 ..= <u32 as MaxValue >:: MAX => " fits in a u32" ,
298
301
_ => " too big" ,
299
302
});
300
303
301
304
```
302
305
303
306
Range patterns are always refutable, even when they cover the complete set
304
- of possible values of a type. For example, ` 0u8... 255u8 ` is refutable even though
307
+ of possible values of a type. For example, ` 0u8..= 255u8 ` is refutable even though
305
308
it covers all possible values of ` u8 ` .
306
309
307
310
## Reference patterns
@@ -358,7 +361,7 @@ subpattern` is needed. For example:
358
361
let x = 2 ;
359
362
360
363
match x {
361
- e @ 1 ... 5 => println! (" got a range element {}" , e ),
364
+ e @ 1 ..= 5 => println! (" got a range element {}" , e ),
362
365
_ => println! (" anything" ),
363
366
}
364
367
```
@@ -394,7 +397,7 @@ the value's fields. For example:
394
397
# age: u8,
395
398
# }
396
399
# let value = Person{ name: String::from("John"), age: 23 };
397
- if let Person{& name: person_name, age: 18... 150} = value { }
400
+ if let Person{& name: person_name, age: 18..= 150} = value { }
398
401
```
399
402
400
403
is not valid. What we must do is:
@@ -405,7 +408,7 @@ is not valid. What we must do is:
405
408
# age : u8 ,
406
409
# }
407
410
# let value = Person { name : String :: from (" John" ), age : 23 };
408
- if let Person {name : ref person_name , age : 18 ... 150 } = value { }
411
+ if let Person {name : ref person_name , age : 18 ..= 150 } = value { }
409
412
```
410
413
411
414
Thus, ` ref ` is not something that is being matched against. Its objective is
0 commit comments