@@ -1459,6 +1459,86 @@ impl<T> [T] {
14591459 m >= n && needle == & self [ m - n..]
14601460 }
14611461
1462+ /// Returns a subslice with the prefix removed.
1463+ ///
1464+ /// Returns [`None`] if slice does not start with `prefix`.
1465+ ///
1466+ /// # Examples
1467+ ///
1468+ /// ```
1469+ /// #![feature(slice_strip)]
1470+ /// let v = [10, 40, 30];
1471+ /// assert_eq!(v.strip_prefix(&[10]), Some(&[40, 30]));
1472+ /// assert_eq!(v.strip_prefix(&[10, 40]), Some(&[30]));
1473+ /// assert_eq!(v.strip_prefix(&[50]), None);
1474+ /// assert_eq!(v.strip_prefix(&[10, 50]), None);
1475+ /// ```
1476+ ///
1477+ /// This method returns the original slice if `prefix` is an empty slice:
1478+ ///
1479+ /// ```
1480+ /// #![feature(slice_strip)]
1481+ /// let v = &[10, 40, 30];
1482+ /// assert_eq!(v.strip_prefix(&[]), Some(v));
1483+ /// let v: &[u8] = &[];
1484+ /// assert_eq!(v.strip_prefix(&[]), Some(v));
1485+ /// ```
1486+ #[ must_use = "returns the subslice without modifying the original" ]
1487+ #[ unstable( feature = "slice_strip" , issue = "73413" ) ]
1488+ pub fn strip_prefix ( & self , prefix : & [ T ] ) -> Option < & [ T ] >
1489+ where
1490+ T : PartialEq ,
1491+ {
1492+ let n = prefix. len ( ) ;
1493+ if n <= self . len ( ) {
1494+ let ( head, tail) = self . split_at ( n) ;
1495+ if head == prefix {
1496+ return Some ( tail) ;
1497+ }
1498+ }
1499+ None
1500+ }
1501+
1502+ /// Returns a subslice with the suffix removed.
1503+ ///
1504+ /// Returns [`None`] if slice does not end with `suffix`.
1505+ ///
1506+ /// # Examples
1507+ ///
1508+ /// ```
1509+ /// #![feature(slice_strip)]
1510+ /// let v = [10, 40, 30];
1511+ /// assert_eq!(v.strip_suffix(&[30]), Some(&[10, 40]));
1512+ /// assert_eq!(v.strip_suffix(&[40, 30]), Some(&[10]));
1513+ /// assert_eq!(v.strip_suffix(&[50]), None);
1514+ /// assert_eq!(v.strip_suffix(&[50, 30]), None);
1515+ /// ```
1516+ ///
1517+ /// This method returns the original slice if `suffix` is an empty slice:
1518+ ///
1519+ /// ```
1520+ /// #![feature(slice_strip)]
1521+ /// let v = &[10, 40, 30];
1522+ /// assert_eq!(v.strip_suffix(&[]), Some(v));
1523+ /// let v: &[u8] = &[];
1524+ /// assert_eq!(v.strip_suffix(&[]), Some(v));
1525+ /// ```
1526+ #[ must_use = "returns the subslice without modifying the original" ]
1527+ #[ unstable( feature = "slice_strip" , issue = "73413" ) ]
1528+ pub fn strip_suffix ( & self , suffix : & [ T ] ) -> Option < & [ T ] >
1529+ where
1530+ T : PartialEq ,
1531+ {
1532+ let ( len, n) = ( self . len ( ) , suffix. len ( ) ) ;
1533+ if n <= len {
1534+ let ( head, tail) = self . split_at ( len - n) ;
1535+ if tail == suffix {
1536+ return Some ( head) ;
1537+ }
1538+ }
1539+ None
1540+ }
1541+
14621542 /// Binary searches this sorted slice for a given element.
14631543 ///
14641544 /// If the value is found then [`Result::Ok`] is returned, containing the
0 commit comments