Skip to content

Commit daa1992

Browse files
committed
Rework range pattern rules
I was not particularly happy with how these were being presented, as I found it a bit confusing how it was combining some of the ranges, and some of the terminology it was using. I've shifted it so that it describes each of the five range kinds separately, using the same terminology from std (and the grammar). This also just has some slight wording tweaks for clarity.
1 parent d42bb62 commit daa1992

File tree

1 file changed

+35
-27
lines changed

1 file changed

+35
-27
lines changed

src/patterns.md

+35-27
Original file line numberDiff line numberDiff line change
@@ -519,57 +519,65 @@ They comprise a *sigil* (`..` or `..=`) and a bound on one or both sides.
519519
A bound on the left of the sigil is called a *lower bound*.
520520
A bound on the right is called an *upper bound*.
521521

522+
r[patterns.range.exclusive]
523+
The *exclusive range pattern* matches all values from the lower bound up to, but not including the upper bound.
524+
It is written as its lower bound, followed by `..`, followed by the upper bound.
522525

523-
r[patterns.range.closed]
524-
A range pattern with both a lower and upper bound will match all values between and including both of its bounds.
525-
It is written as its lower bound, followed by `..` for end-exclusive or `..=` for end-inclusive, followed by its upper bound.
526+
For example, a pattern `'m'..'p'` will match only `'m'`, `'n'` and `'o'`, specifically **not** including `'p'`.
526527

527-
r[patterns.range.type]
528-
The type of the range pattern is the type unification of its upper and lower bounds.
528+
r[patterns.range.inclusive]
529+
The *inclusive range pattern* matches all values from the lower bound up to and including the upper bound.
530+
It is written as its lower bound, followed by `..=`, followed by the upper bound.
529531

530532
For example, a pattern `'m'..='p'` will match only the values `'m'`, `'n'`, `'o'`, and `'p'`.
531-
Similarly, `'m'..'p'` will match only `'m'`, `'n'` and `'o'`, specifically **not** including `'p'`.
533+
534+
r[patterns.range.from]
535+
The *from range pattern* matches all values greater than or equal to the lower bound.
536+
It is written as its lower bound followed by `..`.
537+
538+
For example, `1..` will match any integer greater than or equal to 1, such as 1, 9, or 9001, or 9007199254740991 (if it is of an appropriate size), but not 0, and not negative numbers for signed integers.
539+
540+
r[patterns.range.to-exclusive]
541+
The *to exclusive range pattern* matches all values less than the upper bound.
542+
It is written as `..` followed by the upper bound.
543+
544+
For example, `..10` will match any integer less than 10, such as 9, 1, 0, and for signed integer types, all negative values.
545+
546+
r[patterns.range.to-inclusive]
547+
The *to inclusive range pattern* matches all values less than or equal to the upper bound.
548+
It is written as `..=` followed by the upper bound.
549+
550+
For example, `..=10` will match any integer less than or equal to 10, such as 10, 1, 0, and for signed integer types, all negative values.
532551

533552
r[patterns.range.constraint-less-than]
534553
The lower bound cannot be greater than the upper bound.
535554
That is, in `a..=b`, a ≤ b must be the case.
536555
For example, it is an error to have a range pattern `10..=0`.
537556

538-
r[patterns.range.open-below]
539-
A range pattern with only a lower bound will match any value greater than or equal to the lower bound.
540-
It is written as its lower bound followed by `..`, and has the same type as its lower bound.
541-
For example, `1..` will match 1, 9, or 9001, or 9007199254740991 (if it is of an appropriate size), but not 0, and not negative numbers for signed integers.
542-
543-
r[patterns.range.open-above]
544-
A range pattern with only an upper bound matches any value less than or equal to the upper bound.
545-
It is written as `..=` for an end-inclusive or `..` for an end-exclusive pattern, followed by its upper bound,
546-
and has the same type as its upper bound.
547-
For example, `..=10` will match 10, 1, 0, and for signed integer types, all negative values, while `..10` will not match 10.
548-
549557
r[patterns.range.bound]
550-
The bounds is written as one of:
558+
A bound is written as one of:
551559

552560
* A character, byte, integer, or float literal.
553561
* A `-` followed by an integer or float literal.
554562
* A [path]
555563

556564
r[patterns.range.constraint-bound-path]
557-
If the bounds is written as a path, after macro resolution, the path must resolve to a constant item of the type `char`, an integer type, or a float type.
565+
If a bound is written as a path, after macro resolution, the path must resolve to a constant item of the type `char`, an integer type, or a float type.
558566

559-
r[patterns.range.value]
560-
The type and value of the bounds is dependent upon how it is written out.
567+
r[patterns.range.type]
568+
The range pattern matches the type of its upper and lower bounds, which must be the same type.
561569

562570
r[patterns.range.path-value]
563-
If the bounds is a [path], the pattern has the type and value of the [constant] the path resolves to.
564-
565-
r[patterns.range.float-restriction]
566-
For float range patterns, the constant may not be a `NaN`.
571+
If a bound is a [path], the bound matches the type and has the value of the [constant] the path resolves to.
567572

568573
r[patterns.range.literal-value]
569-
If it is a literal, it has the type and value of the corresponding [literal expression].
574+
If a bound is a literal, the bound matches the type and has the value of the corresponding [literal expression].
570575

571576
r[patterns.range.negation]
572-
If is a literal preceded by a `-`, it has the same type as the corresponding [literal expression] and the value of [negating] the value of the corresponding literal expression.
577+
If a bound is a literal preceded by a `-`, the bound matches the same type as the corresponding [literal expression] and has the value of [negating] the value of the corresponding literal expression.
578+
579+
r[patterns.range.float-restriction]
580+
For float range patterns, the constant may not be a `NaN`.
573581

574582
Examples:
575583

0 commit comments

Comments
 (0)