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