pub trait RangeIntersect<Rhs = Self>: Sized {
type Intersection;
// Required method
fn intersect(self, rhs: Rhs) -> Self::Intersection;
}๐ฌThis is a nightly-only experimental API. (
range_intersect #165428)Expand description
Computes the intersection of two ranges, returning the most specific range type possible.
This trait is not implemented for all combinations, only the ones with a good
Self::Intersection type available.
However, because (Bound<T>, Bound<T>): RangeIntersect, you can always use
a.into_bounds().intersect(b.into_bounds()) for any two IntoBounds types if needed.
ยงExamples
#![feature(range_intersect)]
use core::range::{Range, RangeFrom, RangeInclusive, RangeIntersect};
assert_eq!(
Range::from(-12..387).intersect(Range::from(0..256)),
Range::from(0..256),
);
assert_eq!(
RangeFrom::from(3..).intersect(..5),
Range::from(3..5),
);
assert_eq!(
RangeInclusive::from(1..=9).intersect(RangeFrom::from(5..)),
RangeInclusive::from(5..=9),
);Combine with is_empty to determine if two ranges overlap:
Required Associated Typesยง
Sourcetype Intersection
๐ฌThis is a nightly-only experimental API. (range_intersect #165428)
type Intersection
range_intersect #165428)The resulting range type of the intersection.
Required Methodsยง
Sourcefn intersect(self, rhs: Rhs) -> Self::Intersection
๐ฌThis is a nightly-only experimental API. (range_intersect #165428)
fn intersect(self, rhs: Rhs) -> Self::Intersection
range_intersect #165428)Computes the intersection of self and rhs.
Dyn Compatibilityยง
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".