@@ -331,6 +331,8 @@ impl<I: Iterator> Peekable<I> {
331331 /// If the closure panics, the next value will always be consumed and dropped
332332 /// even if the panic is caught, because the closure never returned an `Err` value to put back.
333333 ///
334+ /// See also: [`next_if_map_mut`](Self::next_if_map_mut).
335+ ///
334336 /// # Examples
335337 ///
336338 /// Parse the leading decimal number from an iterator of characters.
@@ -419,6 +421,44 @@ impl<I: Iterator> Peekable<I> {
419421 self . peeked = Some ( unpeek) ;
420422 None
421423 }
424+
425+ /// Gives a mutable reference to the next value of the iterator and applies a function `f` to it,
426+ /// returning the result and advancing the iterator if `f` returns `Some`.
427+ ///
428+ /// Otherwise, if `f` returns `None`, the next value is kept for the next iteration.
429+ ///
430+ /// If `f` panics, the item that is consumed from the iterator as if `Some` was returned from `f`.
431+ /// The value will be dropped.
432+ ///
433+ /// This is similar to [`next_if_map`](Self::next_if_map), except ownership of the item is not given to `f`.
434+ /// This can be preferable if `f` would copy the item anyway.
435+ ///
436+ /// # Examples
437+ ///
438+ /// Parse the leading decimal number from an iterator of characters.
439+ /// ```
440+ /// #![feature(peekable_next_if_map)]
441+ /// let mut iter = "125 GOTO 10".chars().peekable();
442+ /// let mut line_num = 0_u32;
443+ /// while let Some(digit) = iter.next_if_map_mut(|c| c.to_digit(10)) {
444+ /// line_num = line_num * 10 + digit;
445+ /// }
446+ /// assert_eq!(line_num, 125);
447+ /// assert_eq!(iter.collect::<String>(), " GOTO 10");
448+ /// ```
449+ #[ unstable( feature = "peekable_next_if_map" , issue = "143702" ) ]
450+ pub fn next_if_map_mut < R > ( & mut self , f : impl FnOnce ( & mut I :: Item ) -> Option < R > ) -> Option < R > {
451+ let unpeek = if let Some ( mut item) = self . next ( ) {
452+ match f ( & mut item) {
453+ Some ( result) => return Some ( result) ,
454+ None => Some ( item) ,
455+ }
456+ } else {
457+ None
458+ } ;
459+ self . peeked = Some ( unpeek) ;
460+ None
461+ }
422462}
423463
424464#[ unstable( feature = "trusted_len" , issue = "37572" ) ]
0 commit comments