Numeric ranges can be constructed by the
.. (exclusive) or
..= (inclusive) operators.
An exclusive range does not include the last (i.e. “end”) value.
type_of() an exclusive range returns
An inclusive range includes the last (i.e. “end”) value.
type_of() an inclusive range returns
Ranges are commonly used in the following scenarios.
|Array range-based API’s|
|BLOB range-based API’s|
|String range-based API’s|
The following methods operate on ranges.
|beginning of the range|
|end of the range|
|number to check||does this range contain the specified number?|
|is the range inclusive?|
|is the range exclusive?|
Rust has open-ended ranges, such as
..=end. They are not available in Rhai.
They are not needed because Rhai can overload functions.
Typically, an API accepting ranges as parameters would have equivalent versions that accept a
starting position and a length (the standard
start + len pair), as well as a versions that accept
only the starting position (the length assuming to the end).
In fact, usually all versions redirect to a call to one single version.
Therefore, there should always be a function that can do what open-ended ranges are intended for.
The left-open form (i.e.
..=end) is trivially replaced by using zero as the starting
position with a length that corresponds to the end position (for
The right-open form (i.e.
start..) is trivially replaced by the version taking a single starting position.
let x = [1, 2, 3, 4, 5]; x.extract(0..3); // normal range argument // copies 'x' from positions 0-2 x.extract(2); // copies 'x' from position 2 onwards // equivalent to '2..' x.extract(0, 2); // copies 'x' from beginning for 2 items // equivalent to '..2'