rust/library/coretests/tests
Matthias Krüger 30f9939a0c
Rollup merge of #148941 - stabilize-map-if, r=jhpratt
stabilize `Peekable::next_if_map` (`#![feature(peekable_next_if_map)]`)

# Stabilization report

## Summary

`#![feature(peekable_next_if_map)]` is a variation  of `next_if` on peekable iterators that can transform the peeked item. This creates a way to take ownership of the next item in an iterator when some condition holds, but put the item back when the condition doesn't hold. This pattern would otherwise have needed unwraps in many cases.

[Tracking issue](https://github.com/rust-lang/rust/issues/143702)

### What is stabilized

```rust
impl<I: Iterator> Peekable<I> {
    pub fn next_if_map<R>(
        &mut self,
        f: impl FnOnce(I::Item) -> Result<R, I::Item>,
    ) -> Option<R> {
        ..
    }
    pub fn next_if_map_mut<R>(
        &mut self,
        f: impl FnOnce(&mut I::Item) -> Option<R>,
    ) -> Option<R> {
        ..
    }
}
```

Example usage adapted from the ACP:

```rust
let mut it = Peekable::new("123".chars());

while let Some(digit) = it.next_if_map(|c| c.to_digit(10).ok_or(c)) {
    codepoint = codepoint * 10 + digit;
}
```

or with `next_if_map_mut`:

```rust
let mut it = Peekable::new("123".chars());

while let Some(digit) = iter.next_if_map_mut(|c| c.to_digit(10)) {
    line_num = line_num * 10 + digit;
}
```

Note that the major difference here is that `next_if_map_mut` does not get owned items from the iterator, but mutable references. With that api, the closure can return an `Option` which avoids an `ok_or`. This may require cloning or copying the iterator elements, so if that is expensive, the owned version, `next_if_map`, may be preferable.

### Nightly use

At the moment, this feature is barely used in nightly, though I've found multiple good uses for it in my own projects, hence my pushing for stabilization. It makes the kind of patterns used in recursive descent parsing super concise and maybe with its stabilization it will find more use.

### Test coverage

Besides a quite comprehensive doctest, this feature is tested (including panicking in the closure) here:

c880acdd31/library/coretests/tests/iter/adapters/peekable.rs (L275-L359)

## History

- ACP: https://github.com/rust-lang/libs-team/issues/613 accepted with https://github.com/rust-lang/libs-team/issues/613#issuecomment-3049844223
- implementation: https://github.com/rust-lang/rust/pull/143725 with tests, and no issues reported since july.

## Acknowledgments

ACP, implementation and tracking issue for this feature all by @kennytm <3
2026-01-11 09:56:37 +01:00
..
async_iter Put all coretests in a separate crate 2025-01-26 10:26:36 +00:00
ffi Delegate <CStr as Debug> to ByteStr 2025-06-12 12:53:14 -04:00
floats Unify and deduplicate max recip float tests 2025-10-25 17:57:06 +02:00
fmt move a ui test to coretests unit test 2025-12-22 21:37:18 -05:00
hash Improve core::hash coverage 2025-09-12 13:54:15 -05:00
io Revert #148937 (Remove initialized-bytes tracking from BorrowedBuf and BorrowedCursor) 2025-12-17 14:34:56 +00:00
iter iter::ArrayChunks::into_remainder ought not return option 2025-11-20 15:27:57 +07:00
net Recognise new IPv6 non-global range from RFC9602 2025-03-21 17:53:29 +11:00
num test: update duplicate many_digits test to use f64 instead of f32 2025-12-07 16:51:27 -05:00
ops moved simple test to coretests, introduced more fleshed out doctests for break_ok/continue_ok 2025-04-26 12:57:12 +02:00
panic updated line number in test 2025-08-02 17:34:59 -04:00
alloc.rs Improve core::alloc coverage 2025-09-12 10:49:52 -05:00
any.rs Add opaque TypeId handles for CTFE 2025-07-09 16:37:11 +00:00
array.rs redo the drain 2025-11-27 20:18:13 +07:00
ascii.rs Improve core::ascii coverage 2025-09-16 10:05:40 -05:00
ascii_char.rs Implement Extend<AsciiChar> for String 2025-02-08 16:51:04 -05:00
asserting.rs Put all coretests in a separate crate 2025-01-26 10:26:36 +00:00
atomic.rs Improve core::sync::atomic coverage 2025-09-16 10:26:52 -05:00
bool.rs Add methods for converting bool to Result<(), E> 2025-06-19 23:20:06 +02:00
bstr.rs Actually run the bstr test 2025-01-26 10:45:09 +00:00
cell.rs make RefCell unstably const 2025-06-25 17:06:49 -07:00
char.rs Stabilize char_max_len 2025-11-14 18:23:19 +03:00
clone.rs Put all coretests in a separate crate 2025-01-26 10:26:36 +00:00
cmp.rs Constify Eq, Ord, PartialOrd 2025-09-12 12:39:31 +03:00
const_ptr.rs Put all coretests in a separate crate 2025-01-26 10:26:36 +00:00
convert.rs add a coretest checking TryInto/TryFrom impls 2025-12-01 16:42:52 +01:00
error.rs Put all coretests in a separate crate 2025-01-26 10:26:36 +00:00
ffi.rs Put all coretests in a separate crate 2025-01-26 10:26:36 +00:00
future.rs Put all coretests in a separate crate 2025-01-26 10:26:36 +00:00
hint.rs const select_unpredictable 2025-10-26 21:33:00 -04:00
index.rs add SliceIndex wrapper types Last and Clamp<Idx> 2025-09-15 15:25:12 -04:00
intrinsics.rs Introduce vtable_for intrinsic and use it to implement try_as_dyn and try_as_dyn_mut for fallible coercion from &T / &mut T to &dyn Trait. 2025-12-16 06:39:58 -04:00
lazy.rs Put all coretests in a separate crate 2025-01-26 10:26:36 +00:00
lib.rs Rollup merge of #148941 - stabilize-map-if, r=jhpratt 2026-01-11 09:56:37 +01:00
macros.rs Add regression test for matches! + non_exhaustive_omitted_patterns lint 2025-07-23 14:35:44 +02:00
manually_drop.rs Move const_drop_in_place test from ptr to manually_drop module 2025-10-19 14:43:45 -04:00
mem.rs Use an explicit receiver in DropGuard::dismiss 2025-12-13 14:00:44 +04:00
nonzero.rs feat: Change return type of NonZero::bit_width 2025-11-19 03:17:38 +09:00
ops.rs Update library/coretests/tests/ops.rs 2025-09-10 16:50:45 -05:00
option.rs Clean up some resolved test regressions of const trait removals in std 2025-08-08 00:58:54 +09:00
panic.rs Put all coretests in a separate crate 2025-01-26 10:26:36 +00:00
pattern.rs Put all coretests in a separate crate 2025-01-26 10:26:36 +00:00
pin.rs Put pin!() tests in the right file. 2025-03-28 10:18:04 +01:00
pin_macro.rs update cfg(bootstrap) 2025-05-12 15:33:37 +02:00
ptr.rs const-eval: fix and re-enable pointer fragment support 2025-11-15 10:09:42 +01:00
result.rs Constify additional Result functions 2025-08-01 08:55:50 +03:00
simd.rs Put all coretests in a separate crate 2025-01-26 10:26:36 +00:00
slice.rs slice iter: more cleanup 2025-11-14 17:45:23 +00:00
str.rs tidy: Fix paths to coretests and alloctests 2025-04-05 12:15:49 -07:00
str_lossy.rs Match <OsString as Debug>::fmt to that of str 2025-11-17 16:05:00 -05:00
task.rs Put all coretests in a separate crate 2025-01-26 10:26:36 +00:00
time.rs Implement round-ties-to-even for Duration Debug for consistency with f64 2025-12-22 08:59:20 -05:00
tuple.rs Remove unnecessary parentheses in assert!s 2025-08-10 21:59:04 +00:00
unicode.rs Revert "unicode_data refactors RUST-147622" 2025-11-03 19:53:11 +08:00
waker.rs Put all coretests in a separate crate 2025-01-26 10:26:36 +00:00
wtf8.rs Move WTF-8 code from std to core/alloc 2025-08-20 20:31:33 -04:00