-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add custom nth_back to Skip #60454
Add custom nth_back to Skip #60454
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2280,6 +2280,40 @@ fn test_skip_try_folds() { | |
assert_eq!(iter.next_back(), Some(24)); | ||
} | ||
|
||
#[test] | ||
fn test_skip_nth_back() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think the comment above was addressed; if disagree please add a test that demonstrates that the underlying iterator is still advanced. (You could do that with inspect, or something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I didn't understand you meant that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're now calling |
||
let xs = [0, 1, 2, 3, 4, 5]; | ||
let mut it = xs.iter().skip(2); | ||
assert_eq!(it.nth_back(0), Some(&5)); | ||
assert_eq!(it.nth_back(1), Some(&3)); | ||
assert_eq!(it.nth_back(0), Some(&2)); | ||
assert_eq!(it.nth_back(0), None); | ||
|
||
let ys = [2, 3, 4, 5]; | ||
let mut ity = ys.iter(); | ||
let mut it = xs.iter().skip(2); | ||
assert_eq!(it.nth_back(1), ity.nth_back(1)); | ||
assert_eq!(it.clone().nth(0), ity.clone().nth(0)); | ||
assert_eq!(it.nth_back(0), ity.nth_back(0)); | ||
assert_eq!(it.clone().nth(0), ity.clone().nth(0)); | ||
assert_eq!(it.nth_back(0), ity.nth_back(0)); | ||
assert_eq!(it.clone().nth(0), ity.clone().nth(0)); | ||
assert_eq!(it.nth_back(0), ity.nth_back(0)); | ||
assert_eq!(it.clone().nth(0), ity.clone().nth(0)); | ||
|
||
let mut it = xs.iter().skip(2); | ||
assert_eq!(it.nth_back(4), None); | ||
assert_eq!(it.nth_back(0), None); | ||
|
||
let mut it = xs.iter(); | ||
it.by_ref().skip(2).nth_back(3); | ||
assert_eq!(it.next_back(), Some(&1)); | ||
|
||
let mut it = xs.iter(); | ||
it.by_ref().skip(2).nth_back(10); | ||
assert_eq!(it.next_back(), Some(&1)); | ||
} | ||
|
||
#[test] | ||
fn test_take_try_folds() { | ||
let f = &|acc, x| i32::checked_add(2*acc, x); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In case
n >= self.len() && self.len() != 0
, additionally you need to doself.iter.nth_back(self.len() - 1);
(or whatever that takesself.len()
-items from the back) since the underlying iterator could have side effects.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why
let _ =
and not just the call tonth_back
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was just for expressing intent to throw away the return value of
nth_back
, and I don't meant it is actually needed.