pub struct Interval<T>(/* private fields */);
Expand description
Implementations§
Source§impl<T> Interval<T>
impl<T> Interval<T>
Sourcepub fn write_fmt_with<F>(
&self,
f: &mut Formatter<'_>,
write_fn: F,
) -> Result<(), Error>
pub fn write_fmt_with<F>( &self, f: &mut Formatter<'_>, write_fn: F, ) -> Result<(), Error>
Writes the Interval
to the given Formatter
using a specified
function to write the interval’s boundary points.
Sourcepub fn from_str_with<F, E>(
s: &str,
read_fn: F,
) -> Result<Self, IntervalParseError<E>>
pub fn from_str_with<F, E>( s: &str, read_fn: F, ) -> Result<Self, IntervalParseError<E>>
Parses an Interval
from a string using the specified function to
parse the interval’s boundary points.
Source§impl<T> Interval<T>
impl<T> Interval<T>
Sourcepub fn new(left: Bound<T>, right: Bound<T>) -> Self
pub fn new(left: Bound<T>, right: Bound<T>) -> Self
Constructs a new Interval
from the given Bound
s.
§Examples
let interval: Interval<i32> = Interval::new(Include(3), Exclude(7));
Finite
types will have their bounds closed:
let interval: Interval<i32> = Interval::new(Exclude(-3), Exclude(7));
assert_eq!(interval, Interval::new(Include(-2), Include(6)));
If the bounds are out of order, and empty Interval
will be returned.
let interval: Interval<i32> = Interval::new(Exclude(7), Exclude(-7));
assert_eq!(interval, Interval::empty());
Sourcepub fn point(point: T) -> Self
pub fn point(point: T) -> Self
Constructs a new degenerate Interval
containing the given point.
§Example
let interval: Interval<i32> = Interval::point(3);
Sourcepub fn open(left: T, right: T) -> Self
pub fn open(left: T, right: T) -> Self
Constructs a new bounded open Interval
from the given points.
§Examples
let interval: Interval<i32> = Interval::open(3, 7);
Finite
types will have their bounds closed:
let interval: Interval<i32> = Interval::open(-3, 7);
assert_eq!(interval, Interval::new(Include(-2), Include(6)));
If the bounds are out of order, and empty Interval
will be returned.
let interval: Interval<i32> = Interval::open(7, -7);
assert_eq!(interval, Interval::empty());
Sourcepub fn left_open(left: T, right: T) -> Self
pub fn left_open(left: T, right: T) -> Self
Constructs a new bounded left-open Interval
from the given points.
§Examples
let interval: Interval<i32> = Interval::left_open(3, 7);
Finite
types will have their bounds closed:
let interval: Interval<i32> = Interval::left_open(-3, 7);
assert_eq!(interval, Interval::new(Include(-2), Include(7)));
If the bounds are out of order, and empty Interval
will be returned.
let interval: Interval<i32> = Interval::left_open(7, -7);
assert_eq!(interval, Interval::empty());
If the bounds are identical, a point Interval
will be returned.
let interval: Interval<i32> = Interval::left_open(5, 5);
assert_eq!(interval, Interval::point(5));
Sourcepub fn right_open(left: T, right: T) -> Self
pub fn right_open(left: T, right: T) -> Self
Constructs a new bounded right-open Interval
from the given points.
§Examples
let interval: Interval<i32> = Interval::right_open(3, 7);
Finite
types will have their bounds closed:
let interval: Interval<i32> = Interval::right_open(-3, 7);
assert_eq!(interval, Interval::new(Include(-3), Include(6)));
If the bounds are out of order, and empty Interval
will be returned.
let interval: Interval<i32> = Interval::right_open(7, -7);
assert_eq!(interval, Interval::empty());
If the bounds are identical, a point Interval
will be returned.
let interval: Interval<i32> = Interval::right_open(5, 5);
assert_eq!(interval, Interval::point(5));
Sourcepub fn closed(left: T, right: T) -> Self
pub fn closed(left: T, right: T) -> Self
Constructs a new bounded closed Interval
from the given points.
§Examples
let interval: Interval<i32> = Interval::closed(3, 7);
If the bounds are out of order, and empty Interval
will be returned.
let interval: Interval<i32> = Interval::closed(7, -7);
assert_eq!(interval, Interval::empty());
If the bounds are identical, a point Interval
will be returned.
let interval: Interval<i32> = Interval::closed(5, 5);
assert_eq!(interval, Interval::point(5));
Sourcepub fn left_closed(left: T, right: T) -> Self
pub fn left_closed(left: T, right: T) -> Self
Constructs a new bounded left-closed Interval
from the given points.
§Examples
let interval: Interval<i32> = Interval::left_closed(3, 7);
Finite
types will have their bounds closed:
let interval: Interval<i32> = Interval::left_closed(-3, 7);
assert_eq!(interval, Interval::new(Include(-3), Include(6)));
If the bounds are out of order, and empty Interval
will be returned.
let interval: Interval<i32> = Interval::left_closed(7, -7);
assert_eq!(interval, Interval::empty());
If the bounds are identical, a point Interval
will be returned.
let interval: Interval<i32> = Interval::left_closed(5, 5);
assert_eq!(interval, Interval::point(5));
Sourcepub fn right_closed(left: T, right: T) -> Self
pub fn right_closed(left: T, right: T) -> Self
Constructs a new bounded right-closed Interval
from the given points.
§Examples
let interval: Interval<i32> = Interval::right_closed(3, 7);
Finite
types will have their bounds closed:
let interval: Interval<i32> = Interval::right_closed(-3, 7);
assert_eq!(interval, Interval::new(Include(-2), Include(7)));
If the bounds are out of order, and empty Interval
will be returned.
let interval: Interval<i32> = Interval::right_closed(7, -7);
assert_eq!(interval, Interval::empty());
If the bounds are identical, a point Interval
will be returned.
let interval: Interval<i32> = Interval::right_closed(5, 5);
assert_eq!(interval, Interval::point(5));
Sourcepub fn unbounded_from(point: T) -> Self
pub fn unbounded_from(point: T) -> Self
Constructs a new right-unbounded Interval
from (and including) the
given point.
§Examples
let interval: Interval<i32> = Interval::unbounded_from(3);
Finite
types will have their bounds closed:
let interval: Interval<i32> = Interval::unbounded_from(7);
assert_eq!(interval, Interval::new(Include(7), Include(i32::MAX)));
Sourcepub fn unbounded_to(point: T) -> Self
pub fn unbounded_to(point: T) -> Self
Constructs a new left-unbounded Interval
to (and including) the
given point.
§Examples
let interval: Interval<i32> = Interval::unbounded_to(3);
Finite
types will have their bounds closed:
let interval: Interval<i32> = Interval::unbounded_to(7);
assert_eq!(interval, Interval::new(Include(i32::MIN), Include(7)));
Sourcepub fn unbounded_up_from(point: T) -> Self
pub fn unbounded_up_from(point: T) -> Self
Constructs a new right-unbounded Interval
from (but excluding) the
given point.
§Examples
let interval: Interval<i32> = Interval::unbounded_up_from(3);
Finite
types will have their bounds closed:
let interval: Interval<i32> = Interval::unbounded_up_from(7);
assert_eq!(interval, Interval::new(Include(8), Include(i32::MAX)));
Sourcepub fn unbounded_up_to(point: T) -> Self
pub fn unbounded_up_to(point: T) -> Self
Constructs a new left-unbounded Interval
to (but excluding) the
given point.
§Examples
let interval: Interval<i32> = Interval::unbounded_up_to(3);
Finite
types will have their bounds closed:
let interval: Interval<i32> = Interval::unbounded_up_to(7);
assert_eq!(interval, Interval::new(Include(i32::MIN), Include(6)));
Sourcepub fn into_non_empty(self) -> Option<Self>
pub fn into_non_empty(self) -> Option<Self>
Converts the Interval
into an Option
, returning None
if it is
empty.
let interval: Interval<i32> = Interval::closed(0, 4);
assert_eq!(interval.into_non_empty(), Some(Interval::closed(0, 4)));
let interval: Interval<i32> = Interval::empty();
assert_eq!(interval.into_non_empty(), None);
Sourcepub fn bounds(&self) -> Option<(Bound<T>, Bound<T>)>
pub fn bounds(&self) -> Option<(Bound<T>, Bound<T>)>
Returns the lower and upper Bound
s of the Interval
, or None
if
the Interval
is empty
.
§Examples
let interval: Interval<i32> = Interval::closed(-3, 5);
assert_eq!(interval.bounds(), Some((Include(-3), Include(5))));
Finite
types will have their bounds closed:
let interval: Interval<i32> = Interval::open(-3, 5);
assert_eq!(interval.bounds(), Some((Include(-2), Include(4))));
Sourcepub fn lower_bound(&self) -> Option<Bound<T>>
pub fn lower_bound(&self) -> Option<Bound<T>>
Returns the lower Bound
of the Interval
, or None
if the
Interval
is empty
.
§Examples
let interval: Interval<i32> = Interval::closed(-3, 5);
assert_eq!(interval.lower_bound(), Some(Include(-3)));
Finite
types will have their bounds closed:
let interval: Interval<i32> = Interval::open(-3, 5);
assert_eq!(interval.lower_bound(), Some(Include(-2)));
Sourcepub fn upper_bound(&self) -> Option<Bound<T>>
pub fn upper_bound(&self) -> Option<Bound<T>>
Returns the upper Bound
of the Interval
, or None
if the
Interval
is empty
.
§Examples
let interval: Interval<i32> = Interval::closed(-3, 5);
assert_eq!(interval.upper_bound(), Some(Include(5)));
Finite
types will have their bounds closed:
let interval: Interval<i32> = Interval::open(-3, 5);
assert_eq!(interval.upper_bound(), Some(Include(4)));
Sourcepub fn infimum(&self) -> Option<T>
pub fn infimum(&self) -> Option<T>
Returns the greatest lower bound of the Interval
, or None
if the
Interval
is empty
or unbounded below.
§Examples
let interval: Interval<i32> = Interval::closed(-3, 5);
assert_eq!(interval.infimum(), Some(-3));
Finite
types will have their bounds closed:
let interval: Interval<i32> = Interval::open(-3, 5);
assert_eq!(interval.infimum(), Some(-2));
Sourcepub fn supremum(&self) -> Option<T>
pub fn supremum(&self) -> Option<T>
Returns the least upper bound of the Interval
, or None
if the
Interval
is empty
or unbounded above.
§Examples
let interval: Interval<i32> = Interval::closed(-3, 5);
assert_eq!(interval.supremum(), Some(5));
Finite
types will have their bounds closed:
let interval: Interval<i32> = Interval::open(-3, 5);
assert_eq!(interval.supremum(), Some(4));
Sourcepub fn extrema(&self) -> Option<(T, T)>
pub fn extrema(&self) -> Option<(T, T)>
Returns the greatest lower bound and least upper bound of the
Interval
, or None
if the Interval
is empty
or unbounded.
§Examples
let interval: Interval<i32> = Interval::closed(-3, 5);
assert_eq!(interval.extrema(), Some((-3, 5)));
Finite
types will have their bounds closed:
let interval: Interval<i32> = Interval::open(-3, 5);
assert_eq!(interval.extrema(), Some((-2, 4)));
Sourcepub fn size(&self) -> Option<T>where
T: Sub<Output = T>,
pub fn size(&self) -> Option<T>where
T: Sub<Output = T>,
Returns the size of the Interval
, or None
if it is either infinite
or empty.
§Example
let interval: Interval<i32> = Interval::closed(-3, 7);
assert_eq!(interval.size(), Some(10));
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
if the interval contains no points.
§Example
let interval: Interval<i32> = Interval::closed(-3, 5);
assert_eq!(interval.is_empty(), false);
let interval: Interval<i32> = Interval::empty();
assert_eq!(interval.is_empty(), true);
Sourcepub fn is_degenerate(&self) -> bool
pub fn is_degenerate(&self) -> bool
Returns true
if the interval contains a single point.
§Example
let interval: Interval<i32> = Interval::closed(-3, 5);
assert_eq!(interval.is_degenerate(), false);
let interval: Interval<i32> = Interval::point(4);
assert_eq!(interval.is_degenerate(), true);
Sourcepub fn is_proper(&self) -> bool
pub fn is_proper(&self) -> bool
Returns true
if the interval contains more than one point.
§Example
let interval: Interval<i32> = Interval::closed(-3, 5);
assert_eq!(interval.is_proper(), true);
let interval: Interval<i32> = Interval::point(4);
assert_eq!(interval.is_proper(), false);
let interval: Interval<i32> = Interval::empty();
assert_eq!(interval.is_proper(), false);
Sourcepub fn is_open(&self) -> bool
pub fn is_open(&self) -> bool
Returns true
if the interval is open.
§Examples
let interval: Interval<i32> = Interval::left_open(-3, 5);
assert_eq!(interval.is_open(), false);
let interval: Interval<i32> = Interval::point(4);
assert_eq!(interval.is_open(), false);
Note that the empty and full intervals are open:
let interval: Interval<i32> = Interval::empty();
assert_eq!(interval.is_open(), true);
let interval: Interval<i32> = Interval::full();
assert_eq!(interval.is_open(), false);
Sourcepub fn is_left_open(&self) -> bool
pub fn is_left_open(&self) -> bool
Returns true
if the interval is left-open.
§Examples
let interval: Interval<i32> = Interval::left_open(-3, 5);
assert_eq!(interval.is_left_open(), false);
let interval: Interval<i32> = Interval::closed(2, 4);
assert_eq!(interval.is_left_open(), false);
Note that the left-unbounded intervals are considered left-open:
let interval: Interval<i32> = Interval::unbounded_to(4);
assert_eq!(interval.is_left_open(), false);
let interval: Interval<i32> = Interval::full();
assert_eq!(interval.is_left_open(), false);
Sourcepub fn is_right_open(&self) -> bool
pub fn is_right_open(&self) -> bool
Returns true
if the interval is right-open.
§Examples
let interval: Interval<i32> = Interval::right_open(-3, 5);
assert_eq!(interval.is_right_open(), false);
let interval: Interval<i32> = Interval::closed(2, 4);
assert_eq!(interval.is_right_open(), false);
Note that the right-unbounded intervals are considered right-open:
let interval: Interval<i32> = Interval::unbounded_from(4);
assert_eq!(interval.is_right_open(), false);
let interval: Interval<i32> = Interval::full();
assert_eq!(interval.is_right_open(), false);
Sourcepub fn is_half_open(&self) -> bool
pub fn is_half_open(&self) -> bool
Returns true
if the interval is half-open.
§Examples
let interval: Interval<i32> = Interval::left_open(-3, 5);
assert_eq!(interval.is_half_open(), false);
let interval: Interval<i32> = Interval::closed(2, 4);
assert_eq!(interval.is_half_open(), false);
Note that the half-unbounded intervals are considered half-open:
let interval: Interval<i32> = Interval::unbounded_to(4);
assert_eq!(interval.is_half_open(), false);
let interval: Interval<i32> = Interval::full();
assert_eq!(interval.is_half_open(), false);
Sourcepub fn is_closed(&self) -> bool
pub fn is_closed(&self) -> bool
Returns true
if the interval is closed.
§Examples
let interval: Interval<i32> = Interval::closed(-3, 5);
assert_eq!(interval.is_closed(), true);
let interval: Interval<i32> = Interval::left_open(-2, 4);
assert_eq!(interval.is_closed(), true);
Note that the empty and full intervals are closed:
let interval: Interval<i32> = Interval::empty();
assert_eq!(interval.is_closed(), true);
let interval: Interval<i32> = Interval::full();
assert_eq!(interval.is_closed(), true);
Sourcepub fn is_left_closed(&self) -> bool
pub fn is_left_closed(&self) -> bool
Returns true
if the interval is left-closed.
§Example
let interval: Interval<i32> = Interval::closed(-3, 5);
assert_eq!(interval.is_left_closed(), true);
let interval: Interval<i32> = Interval::left_open(-2, 4);
assert_eq!(interval.is_left_closed(), true);
Sourcepub fn is_right_closed(&self) -> bool
pub fn is_right_closed(&self) -> bool
Returns true
if the interval is right-closed.
§Example
let interval: Interval<i32> = Interval::closed(-3, 5);
assert_eq!(interval.is_right_closed(), true);
let interval: Interval<i32> = Interval::right_open(-2, 4);
assert_eq!(interval.is_right_closed(), true);
Sourcepub fn is_half_closed(&self) -> bool
pub fn is_half_closed(&self) -> bool
Returns true
if the interval is half-closed.
§Example
let interval: Interval<i32> = Interval::unbounded_to(-3);
assert_eq!(interval.is_half_closed(), false);
let interval: Interval<i32> = Interval::open(-2, 4);
assert_eq!(interval.is_half_closed(), false);
Sourcepub fn is_bounded(&self) -> bool
pub fn is_bounded(&self) -> bool
Returns true
if the the interval is bounded.
§Example
let interval: Interval<i32> = Interval::open(-2, 4);
assert_eq!(interval.is_left_bounded(), true);
let interval: Interval<i32> = Interval::unbounded_to(-3);
assert_eq!(interval.is_left_bounded(), true);
Sourcepub fn is_left_bounded(&self) -> bool
pub fn is_left_bounded(&self) -> bool
Returns true
if the the interval is left-bounded.
§Example
let interval: Interval<i32> = Interval::open(-2, 4);
assert_eq!(interval.is_left_bounded(), true);
let interval: Interval<i32> = Interval::unbounded_to(-3);
assert_eq!(interval.is_left_bounded(), true);
Sourcepub fn is_right_bounded(&self) -> bool
pub fn is_right_bounded(&self) -> bool
Returns true
if the the interval is right-bounded.
§Example
let interval: Interval<i32> = Interval::open(-2, 4);
assert_eq!(interval.is_right_bounded(), true);
let interval: Interval<i32> = Interval::unbounded_from(-3);
assert_eq!(interval.is_right_bounded(), true);
Sourcepub fn is_half_bounded(&self) -> bool
pub fn is_half_bounded(&self) -> bool
Returns true
if the the interval is helf-bounded.
§Example
let interval: Interval<i32> = Interval::unbounded_to(-2);
assert_eq!(interval.is_half_bounded(), false);
let interval: Interval<i32> = Interval::full();
assert_eq!(interval.is_half_bounded(), false);
Sourcepub fn contains(&self, point: &T) -> bool
pub fn contains(&self, point: &T) -> bool
Returns true
if the the interval contains the given point.
§Example
let interval: Interval<i32> = Interval::closed(0, 20);
assert_eq!(interval.contains(&2), true);
assert_eq!(interval.contains(&-15), false);
Sourcepub fn intersects(&self, other: &Self) -> bool
pub fn intersects(&self, other: &Self) -> bool
Returns true
if the Interval
overlaps the given Interval
.
§Example
let a: Interval<i32> = Interval::closed(-3, 5);
let b: Interval<i32> = Interval::closed(4, 15);
assert_eq!(a.intersects(&b), true);
let a: Interval<i32> = Interval::closed(-3, 5);
let b: Interval<i32> = Interval::closed(8, 12);
assert_eq!(a.intersects(&b), false);
Sourcepub fn is_adjacent_to(&self, other: &Self) -> bool
pub fn is_adjacent_to(&self, other: &Self) -> bool
Returns true
if the Interval
shares a bound with the given
Interval
.
§Example
let a: Interval<i32> = Interval::closed(-3, 5);
let b: Interval<i32> = Interval::closed(5, 15);
assert_eq!(a .is_adjacent_to(&b), true);
let a: Interval<i32> = Interval::closed(-3, 5);
let b: Interval<i32> = Interval::closed(8, 12);
assert_eq!(a .is_adjacent_to(&b), false);
Sourcepub fn complement(&self) -> impl Iterator<Item = Self>
pub fn complement(&self) -> impl Iterator<Item = Self>
Sourcepub fn intersect(&self, other: &Self) -> Self
pub fn intersect(&self, other: &Self) -> Self
Returns the largest Interval
whose points are all contained entirely
within the Interval
and the given Interval
.
§Example
let a: Interval<i32> = Interval::closed(-3, 7);
let b: Interval<i32> = Interval::closed(4, 13);
assert_eq!(a.intersect(&b), Interval::closed(4, 7));
Sourcepub fn union(&self, other: &Self) -> impl Iterator<Item = Self>
pub fn union(&self, other: &Self) -> impl Iterator<Item = Self>
Returns the Interval
s containing all points in the Interval
and the
given Interval
.
§Example
let a: Interval<i32> = Interval::closed(-3, 7);
let b: Interval<i32> = Interval::closed(4, 13);
assert_eq!(a.union(&b).collect::<Vec<_>>(),
[Interval::closed(-3, 13)]);
Sourcepub fn minus(&self, other: &Self) -> impl Iterator<Item = Self>
pub fn minus(&self, other: &Self) -> impl Iterator<Item = Self>
Returns the Interval
s containing all points in the Interval
which
are not in the given Interval
.
§Example
let a: Interval<i32> = Interval::closed(-3, 7);
let b: Interval<i32> = Interval::closed(4, 13);
assert_eq!(a.minus(&b).collect::<Vec<_>>(),
[Interval::right_open(-3, 4)]);
Sourcepub fn enclose(&self, other: &Self) -> Self
pub fn enclose(&self, other: &Self) -> Self
Returns the smallest Interval
that contains all of the points
contained within the Interval
and the given Interval
.
§Example
let a: Interval<i32> = Interval::closed(-3, 5);
let b: Interval<i32> = Interval::closed(9, 13);
assert_eq!(a.enclose(&b), Interval::closed(-3, 13));
Source§impl<T> Interval<T>
impl<T> Interval<T>
Sourcepub fn iter(&self) -> Iter<T> ⓘ
pub fn iter(&self) -> Iter<T> ⓘ
Returns an Iterator
over the points in the Interval
. Only defined
for Finite
Interval
s.
§Examples
let interval: Interval<i32> = Interval::open(3, 7);
assert_eq!(interval.iter().collect::<Vec<_>>(), [4, 5, 6]);
The Interval
can be iterated in both directions.
let interval: Interval<i32> = Interval::open(3, 7);
assert_eq!(interval.iter().rev().collect::<Vec<_>>(), [6, 5, 4]);
Trait Implementations§
Source§impl<T> Extend<Interval<T>> for Selection<T>
impl<T> Extend<Interval<T>> for Selection<T>
Source§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = Interval<T>>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = Interval<T>>,
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)