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) -> any

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: any) -> 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);

Trait Implementations

impl PartialEq for Range
fn eq(value: any, value1: any) -> bool

Compare two values for equality.

Examples

assert_eq!(1.eq(2), false);
assert_eq!(2.eq(2), true);
assert_eq!(2.eq(1), false);
fn ne(value: any, value1: any) -> bool

Compare two values for inequality.

Examples

assert_eq!(1.ne(2), true);
assert_eq!(2.ne(2), false);
assert_eq!(2.ne(1), true);
impl Eq for Range
impl PartialOrd for Range
fn partial_cmp(value: any, value1: any) -> Option

Compare two values.

Examples

use std::cmp::Ordering;

assert_eq!(1.partial_cmp(2), Some(Ordering::Less));
assert_eq!(2.partial_cmp(2), Some(Ordering::Equal));
assert_eq!(2.partial_cmp(1), Some(Ordering::Greater));
fn lt(value: any, value1: any) -> bool

Tests less than (for self and other) and is used by the < operator.

Examples

assert_eq!(1.0 < 1.0, false);
assert_eq!(1.0 < 2.0, true);
assert_eq!(2.0 < 1.0, false);
fn le(value: any, value1: any) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator.

Examples

assert_eq!(1.0 <= 1.0, true);
assert_eq!(1.0 <= 2.0, true);
assert_eq!(2.0 <= 1.0, false);
fn gt(value: any, value1: any) -> bool

Tests greater than (for self and other) and is used by the > operator.

Examples

assert_eq!(1.0 > 1.0, false);
assert_eq!(1.0 > 2.0, false);
assert_eq!(2.0 > 1.0, true);
fn ge(value: any, value1: any) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator.

Examples

assert_eq!(1.0 >= 1.0, true);
assert_eq!(1.0 >= 2.0, false);
assert_eq!(2.0 >= 1.0, true);
impl Ord for Range
fn cmp(value: any, value1: any) -> Ordering

Compare two values.

Examples

use std::cmp::Ordering;

assert_eq!(1.cmp(2), Ordering::Less);
assert_eq!(2.cmp(2), Ordering::Equal);
assert_eq!(2.cmp(1), Ordering::Greater);
fn min(value: any, value1: any) -> Ordering

Return the minimum of two values.

Examples

assert_eq!(1.min(2), 1);
assert_eq!(2.min(2), 2);
assert_eq!(2.min(1), 1);
fn max(value: any, value1: any) -> Ordering

Return the maximum of two values.

Examples

assert_eq!(1.max(2), 2);
assert_eq!(2.max(2), 2);
assert_eq!(2.max(1), 2);

Protocols

protocol GET start
let $out = value.start

Allows a get operation to work.

protocol SET start
value.start = $input

Allows a set operation to work.

protocol GET end
let $out = 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);
protocol LT
if $a < $b { }

The protocol behind the < operator.

protocol LE
if $a <= $b { }

The protocol behind the <= operator.

protocol GT
if $a > $b { }

The protocol behind the > operator.

protocol GE
if $a >= $b { }

The protocol behind the >= operator.

protocol MIN
$a.min($b)

The implementation protocol for the PartialOrd::min method.

protocol MAX
$a.max($b)

The implementation protocol for the PartialOrd::max method.