Struct ::std::ops::Range

Overview

Type for a range expression start..end.

Examples

let range = 0..10;
assert!(!range.contains(-10));
assert!(range.contains(5));
assert!(!range.contains(10));
assert!(!range.contains(20));

assert!(range is std::ops::Range);

Ranges can contain any type:

let range = 'a'..'f';
assert_eq!(range.start, 'a');
range.start = 'b';
assert_eq!(range.start, 'b');
assert_eq!(range.end, 'f');
range.end = 'g';
assert_eq!(range.end, 'g');

Certain ranges can be used as iterators:

let range = 'a'..'e';
assert_eq!(range.iter().collect::<Vec>(), ['a', 'b', 'c', 'd']);

Examples

use rune::runtime::Range;

let start = rune::to_value(1)?;
let end = rune::to_value(10)?;
let _ = Range::new(start, end);

Methods

fn iter(self) -> Iterator

Iterate over the range.

Panics

This panics if the range is not a well-defined range.

Examples

let range = 'a'..'e';
assert_eq!(range.iter().collect::<Vec>(), ['a', 'b', 'c', 'd']);

Cannot construct an iterator over floats:

let range = 1.0..2.0;
range.iter()
fn contains(self, value) -> bool

Test if the range contains the given value.

The check is performed using the [PARTIAL_CMP] protocol.

Examples

let range = 0..10;

assert!(!range.contains(-10));
assert!(range.contains(5));
assert!(!range.contains(10));
assert!(!range.contains(20));

assert!(range is std::ops::Range);

Protocols

protocol get start
let output = value.start

Allows a get operation to work.

protocol set start
value.start = input

Allows a set operation to work.

protocol get end
let output = value.end

Allows a get operation to work.

protocol set end
value.end = input

Allows a set operation to work.

protocol into_iter
for item in value { }

Iterate over the range.

Panics

This panics if the range is not a well-defined range.

Examples

let vec = [];

for value in 'a'..'e' {
   vec.push(value);
}

assert_eq!(vec, ['a', 'b', 'c', 'd']);

Cannot construct an iterator over floats:

for value in 1.0..2.0 {
}
protocol partial_eq
if value == b { }

Test the range for partial equality.

Examples

let range = 'a'..'e';
assert!(range == ('a'..'e'));
assert!(range != ('b'..'e'));

let range = 1.0..2.0;
assert!(range == (1.0..2.0));
assert!(range != (f64::NAN..2.0));
assert!((f64::NAN..2.0) != (f64::NAN..2.0));
protocol eq
if value == b { }

Test the range for total equality.

Examples

use std::ops::eq;

let range = 'a'..'e';
assert!(eq(range, 'a'..'e'));
assert!(!eq(range, 'b'..'e'));
protocol partial_cmp
if value < b { }

Test the range for partial ordering.

Examples

assert!(('a'..'e') < ('b'..'e'));
assert!(('c'..'e') > ('b'..'e'));
assert!(!((f64::NAN..2.0) > (f64::INFINITY..2.0)));
assert!(!((f64::NAN..2.0) < (f64::INFINITY..2.0)));
protocol cmp
if value < b { }

Test the range for total ordering.

Examples

use std::ops::cmp;
use std::cmp::Ordering;

assert_eq!(cmp('a'..'e', 'b'..'e'), Ordering::Less);
assert_eq!(cmp('c'..'e', 'b'..'e'), Ordering::Greater);