Repository: rust-lang/rust
File: library/coretests/tests/iter/adapters/intersperse.rs (Rust)

Code before:
}

#[test]
fn test_try_fold_specialization_intersperse_err() {
    let orig_iter = ["a", "b"].iter().copied().intersperse("-");

    // Abort after the first item.
    let mut iter = orig_iter.clone();
    iter.try_for_each(|_| None::<()>);
    assert_eq!(iter.next(), Some("-"));
    assert_eq!(iter.next(), Some("b"));
    assert_eq!(iter.next(), None);

    // Abort after the second item.
    let mut iter = orig_iter.clone();
    iter.try_for_each(|item| if item == "-" { None } else { Some(()) });
    assert_eq!(iter.next(), Some("b"));
    assert_eq!(iter.next(), None);

    // Abort after the third item.
    let mut iter = orig_iter.clone();
    iter.try_for_each(|item| if item == "b" { None } else { Some(()) });
    assert_eq!(iter.next(), None);
}

// FIXME: `intersperse` current behavior may change for
// non-fused iterators, so this test will likely have to
// be adjusted; see PR #152855 and issue #79524
// if `intersperse` doesn't change, remove this FIXME.
#[test]
fn test_non_fused_iterator_intersperse() {
    #[derive(Debug)]
    struct TestCounter {
        counter: usize,
    }

    /// Given a counter of 0, this produces:
    /// `None` -> `Some(2)` -> `None` -> `Some(4)` -> `None` -> `Some(6)`
    /// -> and then `None` endlessly
    impl Iterator for TestCounter {
        type Item = usize;
        fn next(&mut self) -> Option<Self::Item> {
            if self.counter > 6 {
                None
            } else if self.counter % 2 == 0 {
                self.counter += 1;
                None
            } else {
                self.counter += 1;
                Some(self.counter)
            }

Review comment (suggestion):
```suggestion
// FIXME(iter_intersperse): `intersperse` current behavior may change for
```

For greppability during stabilization

Code after:
}

#[test]
fn test_try_fold_specialization_intersperse_err() {
    let orig_iter = ["a", "b"].iter().copied().intersperse("-");

    // Abort after the first item.
    let mut iter = orig_iter.clone();
    iter.try_for_each(|_| None::<()>);
    assert_eq!(iter.next(), Some("-"));
    assert_eq!(iter.next(), Some("b"));
    assert_eq!(iter.next(), None);

    // Abort after the second item.
    let mut iter = orig_iter.clone();
    iter.try_for_each(|item| if item == "-" { None } else { Some(()) });
    assert_eq!(iter.next(), Some("b"));
    assert_eq!(iter.next(), None);

    // Abort after the third item.
    let mut iter = orig_iter.clone();
    iter.try_for_each(|item| if item == "b" { None } else { Some(()) });
    assert_eq!(iter.next(), None);
}

// FIXME(iter_intersperse): `intersperse` current behavior may change for
// non-fused iterators, so this test will likely have to
// be adjusted; see PR #152855 and issue #79524
// if `intersperse` doesn't change, remove this FIXME.
#[test]
fn test_non_fused_iterator_intersperse() {
    #[derive(Debug)]
    struct TestCounter {
        counter: usize,
    }

    /// Given a counter of 0, this produces:
    /// `None` -> `Some(2)` -> `None` -> `Some(4)` -> `None` -> `Some(6)`
    /// -> and then `None` endlessly
    impl Iterator for TestCounter {
        type Item = usize;
        fn next(&mut self) -> Option<Self::Item> {
            if self.counter > 6 {
                None
            } else if self.counter % 2 == 0 {
                self.counter += 1;
                None
            } else {
                self.counter += 1;
                Some(self.counter)
            }