Skip to content

Commit

Permalink
Fix behaviour after iterator exhaustion
Browse files Browse the repository at this point in the history
  • Loading branch information
varkor committed Jan 5, 2018
1 parent 439beab commit c23d450
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
12 changes: 9 additions & 3 deletions src/libcore/iter/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,17 +377,23 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {

#[inline]
fn last(self) -> Option<A> {
Some(self.end)
if self.start <= self.end {
Some(self.end)
} else { None }
}

#[inline]
fn min(self) -> Option<A> {
Some(self.start)
if self.start <= self.end {
Some(self.start)
} else { None }
}

#[inline]
fn max(self) -> Option<A> {
Some(self.end)
if self.start <= self.end {
Some(self.end)
} else { None }
}
}

Expand Down
13 changes: 11 additions & 2 deletions src/libcore/tests/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1360,21 +1360,30 @@ fn test_range_max() {
}

#[test]
fn test_range_inc_last_max() {
fn test_range_inclusive_last_max() {
assert_eq!((0..=20).last(), Some(20));
assert_eq!((-20..=0).last(), Some(0));
assert_eq!((5..=5).last(), Some(5));
let mut r = 10..=10;
r.next();
assert_eq!(r.last(), None);

assert_eq!((0..=20).max(), Some(20));
assert_eq!((-20..=0).max(), Some(0));
assert_eq!((5..=5).max(), Some(5));
let mut r = 10..=10;
r.next();
assert_eq!(r.max(), None);
}

#[test]
fn test_range_inc_min() {
fn test_range_inclusive_min() {
assert_eq!((0..=20).min(), Some(0));
assert_eq!((-20..=0).min(), Some(-20));
assert_eq!((5..=5).min(), Some(5));
let mut r = 10..=10;
r.next();
assert_eq!(r.min(), None);
}

#[test]
Expand Down

0 comments on commit c23d450

Please sign in to comment.