Skip to content

Commit

Permalink
Add min and last specialisations for Range
Browse files Browse the repository at this point in the history
  • Loading branch information
varkor committed Jan 9, 2018
1 parent 2d83343 commit 919d643
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
10 changes: 10 additions & 0 deletions src/libcore/iter/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,16 @@ impl<A: Step> Iterator for ops::Range<A> {
None
}

#[inline]
fn last(mut self) -> Option<A> {
self.next_back()
}

#[inline]
fn min(mut self) -> Option<A> {
self.next()
}

#[inline]
fn max(mut self) -> Option<A> {
self.next_back()
Expand Down
19 changes: 15 additions & 4 deletions src/libcore/tests/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1353,10 +1353,14 @@ fn test_range_step() {
}

#[test]
fn test_range_max() {
assert_eq!((0..100).max(), Some(99));
assert_eq!((-20..-10).max(), Some(-11));
assert_eq!((1..1).max(), None);
fn test_range_last_max() {
assert_eq!((0..20).last(), Some(19));
assert_eq!((-20..0).last(), Some(-1));
assert_eq!((5..5).last(), None);

assert_eq!((0..20).max(), Some(19));
assert_eq!((-20..0).max(), Some(-1));
assert_eq!((5..5).max(), None);
}

#[test]
Expand All @@ -1376,6 +1380,13 @@ fn test_range_inclusive_last_max() {
assert_eq!(r.max(), None);
}

#[test]
fn test_range_min() {
assert_eq!((0..20).min(), Some(0));
assert_eq!((-20..0).min(), Some(-20));
assert_eq!((5..5).min(), None);
}

#[test]
fn test_range_inclusive_min() {
assert_eq!((0..=20).min(), Some(0));
Expand Down

0 comments on commit 919d643

Please sign in to comment.