Skip to content

Commit 939015a

Browse files
brauliobzehuss
authored andcommitted
..= patterns ARE being stabilized.
1 parent efdf008 commit 939015a

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

src/expressions/match-expr.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@ Multiple match patterns may be joined with the `|` operator:
7070
# let x = 9;
7171
let message = match x {
7272
0 | 1 => "not many",
73-
2 ... 9 => "a few",
73+
2 ..= 9 => "a few",
7474
_ => "lots"
7575
};
7676

7777
assert_eq!(message, "a few");
7878
```
7979

80-
Please notice that the `2...9` is a [Range Pattern], not a [Range Expression]
80+
Please notice that the `2..=9` is a [Range Pattern], not a [Range Expression]
8181
and, thus, only those types of ranges supported by range patterns can be used
8282
in match arms.
8383

src/patterns.md

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ For example, the pattern used in:
3939
if let
4040
Person {
4141
car: Some(_),
42-
age: person_age @ 13...19,
42+
age: person_age @ 13..=19,
4343
name: ref person_name,
4444
..
4545
} = person
@@ -202,7 +202,8 @@ The wildcard pattern is always irrefutable.
202202

203203
> **<sup>Syntax</sup>**\
204204
> _RangePattern_ :\
205-
> &nbsp;&nbsp; _RangePatternBound_ `...` _RangePatternBound_
205+
> &nbsp;&nbsp;&nbsp;&nbsp; _RangePatternBound_ `..=` _RangePatternBound_\
206+
> &nbsp;&nbsp; | _RangePatternBound_ `...` _RangePatternBound_
206207
>
207208
> _RangePatternBound_ :\
208209
> &nbsp;&nbsp; &nbsp;&nbsp; [CHAR_LITERAL]\
@@ -216,11 +217,13 @@ The wildcard pattern is always irrefutable.
216217
[_QualifiedPathInExpression_]: paths.html
217218

218219
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'`,
220221
`'o'`, and `'p'`. The bounds can be literals or paths that point to constant values.
221222

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.
224227

225228
Range patterns only work on scalar types. The accepted types are:
226229

@@ -235,17 +238,17 @@ Examples:
235238
```rust
236239
# let c = 'f';
237240
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,
241244
_ => false,
242245
};
243246

244247
# let ph = 10;
245248
println!("{}", match ph {
246-
0...6 => "acid",
249+
0..=6 => "acid",
247250
7 => "neutral",
248-
8...14 => "base",
251+
8..=14 => "base",
249252
_ => unreachable!(),
250253
});
251254

@@ -262,9 +265,9 @@ println!("{}", match ph {
262265
# let altitude = 70;
263266
#
264267
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",
268271
_ => "outer space, maybe",
269272
});
270273

@@ -274,7 +277,7 @@ println!("{}", match altitude {
274277
# }
275278
# let n_items = 20_832_425;
276279
# 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 {
278281
println!("It fits and occupies {} bytes", size);
279282
}
280283

@@ -292,16 +295,16 @@ if let size @ binary::MEGA...binary::GIGA = n_items * bytes_per_item {
292295
# }
293296
// using qualified paths:
294297
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",
298301
_ => "too big",
299302
});
300303

301304
```
302305

303306
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
305308
it covers all possible values of `u8`.
306309

307310
## Reference patterns
@@ -358,7 +361,7 @@ subpattern` is needed. For example:
358361
let x = 2;
359362

360363
match x {
361-
e @ 1 ... 5 => println!("got a range element {}", e),
364+
e @ 1 ..= 5 => println!("got a range element {}", e),
362365
_ => println!("anything"),
363366
}
364367
```
@@ -394,7 +397,7 @@ the value's fields. For example:
394397
# age: u8,
395398
# }
396399
# 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 { }
398401
```
399402

400403
is not valid. What we must do is:
@@ -405,7 +408,7 @@ is not valid. What we must do is:
405408
# age: u8,
406409
# }
407410
# 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 { }
409412
```
410413

411414
Thus, `ref` is not something that is being matched against. Its objective is

0 commit comments

Comments
 (0)