@@ -2482,6 +2482,62 @@ impl<T> [T] {
2482
2482
RSplitNMut :: new ( self . rsplit_mut ( pred) , n)
2483
2483
}
2484
2484
2485
+ /// Splits the slice on the first element that matches the specified
2486
+ /// predicate.
2487
+ ///
2488
+ /// If any matching elements are resent in the slice, returns the prefix
2489
+ /// before the match and suffix after. The matching element itself is not
2490
+ /// included. If no elements match, returns `None`.
2491
+ ///
2492
+ /// # Examples
2493
+ ///
2494
+ /// ```
2495
+ /// #![feature(slice_split_once)]
2496
+ /// let s = [1, 2, 3, 2, 4];
2497
+ /// assert_eq!(s.split_once(|&x| x == 2), Some((
2498
+ /// &[1][..],
2499
+ /// &[3, 2, 4][..]
2500
+ /// )));
2501
+ /// assert_eq!(s.split_once(|&x| x == 0), None);
2502
+ /// ```
2503
+ #[ unstable( feature = "slice_split_once" , reason = "newly added" , issue = "112811" ) ]
2504
+ #[ inline]
2505
+ pub fn split_once < F > ( & self , pred : F ) -> Option < ( & [ T ] , & [ T ] ) >
2506
+ where
2507
+ F : FnMut ( & T ) -> bool ,
2508
+ {
2509
+ let index = self . iter ( ) . position ( pred) ?;
2510
+ Some ( ( & self [ ..index] , & self [ index + 1 ..] ) )
2511
+ }
2512
+
2513
+ /// Splits the slice on the last element that matches the specified
2514
+ /// predicate.
2515
+ ///
2516
+ /// If any matching elements are resent in the slice, returns the prefix
2517
+ /// before the match and suffix after. The matching element itself is not
2518
+ /// included. If no elements match, returns `None`.
2519
+ ///
2520
+ /// # Examples
2521
+ ///
2522
+ /// ```
2523
+ /// #![feature(slice_split_once)]
2524
+ /// let s = [1, 2, 3, 2, 4];
2525
+ /// assert_eq!(s.rsplit_once(|&x| x == 2), Some((
2526
+ /// &[1, 2, 3][..],
2527
+ /// &[4][..]
2528
+ /// )));
2529
+ /// assert_eq!(s.rsplit_once(|&x| x == 0), None);
2530
+ /// ```
2531
+ #[ unstable( feature = "slice_split_once" , reason = "newly added" , issue = "112811" ) ]
2532
+ #[ inline]
2533
+ pub fn rsplit_once < F > ( & self , pred : F ) -> Option < ( & [ T ] , & [ T ] ) >
2534
+ where
2535
+ F : FnMut ( & T ) -> bool ,
2536
+ {
2537
+ let index = self . iter ( ) . rposition ( pred) ?;
2538
+ Some ( ( & self [ ..index] , & self [ index + 1 ..] ) )
2539
+ }
2540
+
2485
2541
/// Returns `true` if the slice contains an element with the given value.
2486
2542
///
2487
2543
/// This operation is *O*(*n*).
0 commit comments