@@ -1735,6 +1735,52 @@ impl<T, A: Allocator> VecDeque<T, A> {
1735
1735
}
1736
1736
}
1737
1737
1738
+ /// Removes and returns the first element from the deque if the predicate
1739
+ /// returns `true`, or [`None`] if the predicate returns false or the deque
1740
+ /// is empty (the predicate will not be called in that case).
1741
+ ///
1742
+ /// # Examples
1743
+ ///
1744
+ /// ```
1745
+ /// #![feature(vec_deque_pop_if)]
1746
+ /// use std::collections::VecDeque;
1747
+ ///
1748
+ /// let mut deque: VecDeque<i32> = vec![0, 1, 2, 3, 4].into();
1749
+ /// let pred = |x: &mut i32| *x % 2 == 0;
1750
+ ///
1751
+ /// assert_eq!(deque.pop_front_if(pred), Some(0));
1752
+ /// assert_eq!(deque, [1, 2, 3, 4]);
1753
+ /// assert_eq!(deque.pop_front_if(pred), None);
1754
+ /// ```
1755
+ #[ unstable( feature = "vec_deque_pop_if" , issue = "135889" ) ]
1756
+ pub fn pop_front_if ( & mut self , predicate : impl FnOnce ( & mut T ) -> bool ) -> Option < T > {
1757
+ let first = self . front_mut ( ) ?;
1758
+ if predicate ( first) { self . pop_front ( ) } else { None }
1759
+ }
1760
+
1761
+ /// Removes and returns the last element from the deque if the predicate
1762
+ /// returns `true`, or [`None`] if the predicate returns false or the deque
1763
+ /// is empty (the predicate will not be called in that case).
1764
+ ///
1765
+ /// # Examples
1766
+ ///
1767
+ /// ```
1768
+ /// #![feature(vec_deque_pop_if)]
1769
+ /// use std::collections::VecDeque;
1770
+ ///
1771
+ /// let mut deque: VecDeque<i32> = vec![0, 1, 2, 3, 4].into();
1772
+ /// let pred = |x: &mut i32| *x % 2 == 0;
1773
+ ///
1774
+ /// assert_eq!(deque.pop_back_if(pred), Some(4));
1775
+ /// assert_eq!(deque, [0, 1, 2, 3]);
1776
+ /// assert_eq!(deque.pop_back_if(pred), None);
1777
+ /// ```
1778
+ #[ unstable( feature = "vec_deque_pop_if" , issue = "135889" ) ]
1779
+ pub fn pop_back_if ( & mut self , predicate : impl FnOnce ( & mut T ) -> bool ) -> Option < T > {
1780
+ let first = self . back_mut ( ) ?;
1781
+ if predicate ( first) { self . pop_back ( ) } else { None }
1782
+ }
1783
+
1738
1784
/// Prepends an element to the deque.
1739
1785
///
1740
1786
/// # Examples
0 commit comments