Skip to content

Commit c9a4a4a

Browse files
committed
Clarify behavior of slice prefix/suffix operations in case of equality
Operations such as starts_with, ends_with, strip_prefix and strip_suffix can be either strict (do not consider a slice to be a prefix/suffix of itself) or not. In Rust's case, they are not strict. Add a few phrases to the documentation to clarify this.
1 parent ee933f6 commit c9a4a4a

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

library/core/src/slice/mod.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -2519,14 +2519,15 @@ impl<T> [T] {
25192519
cmp::SliceContains::slice_contains(x, self)
25202520
}
25212521

2522-
/// Returns `true` if `needle` is a prefix of the slice.
2522+
/// Returns `true` if `needle` is a prefix of the slice or equal to the slice.
25232523
///
25242524
/// # Examples
25252525
///
25262526
/// ```
25272527
/// let v = [10, 40, 30];
25282528
/// assert!(v.starts_with(&[10]));
25292529
/// assert!(v.starts_with(&[10, 40]));
2530+
/// assert!(v.starts_with(&v));
25302531
/// assert!(!v.starts_with(&[50]));
25312532
/// assert!(!v.starts_with(&[10, 50]));
25322533
/// ```
@@ -2549,14 +2550,15 @@ impl<T> [T] {
25492550
self.len() >= n && needle == &self[..n]
25502551
}
25512552

2552-
/// Returns `true` if `needle` is a suffix of the slice.
2553+
/// Returns `true` if `needle` is a suffix of the slice or equal to the slice.
25532554
///
25542555
/// # Examples
25552556
///
25562557
/// ```
25572558
/// let v = [10, 40, 30];
25582559
/// assert!(v.ends_with(&[30]));
25592560
/// assert!(v.ends_with(&[40, 30]));
2561+
/// assert!(v.ends_with(&v));
25602562
/// assert!(!v.ends_with(&[50]));
25612563
/// assert!(!v.ends_with(&[50, 30]));
25622564
/// ```
@@ -2582,7 +2584,8 @@ impl<T> [T] {
25822584
/// Returns a subslice with the prefix removed.
25832585
///
25842586
/// If the slice starts with `prefix`, returns the subslice after the prefix, wrapped in `Some`.
2585-
/// If `prefix` is empty, simply returns the original slice.
2587+
/// If `prefix` is empty, simply returns the original slice. If `prefix` is equal to the
2588+
/// original slice, returns an empty slice.
25862589
///
25872590
/// If the slice does not start with `prefix`, returns `None`.
25882591
///
@@ -2592,6 +2595,7 @@ impl<T> [T] {
25922595
/// let v = &[10, 40, 30];
25932596
/// assert_eq!(v.strip_prefix(&[10]), Some(&[40, 30][..]));
25942597
/// assert_eq!(v.strip_prefix(&[10, 40]), Some(&[30][..]));
2598+
/// assert_eq!(v.strip_prefix(&[10, 40, 30]), Some(&[][..]));
25952599
/// assert_eq!(v.strip_prefix(&[50]), None);
25962600
/// assert_eq!(v.strip_prefix(&[10, 50]), None);
25972601
///
@@ -2620,7 +2624,8 @@ impl<T> [T] {
26202624
/// Returns a subslice with the suffix removed.
26212625
///
26222626
/// If the slice ends with `suffix`, returns the subslice before the suffix, wrapped in `Some`.
2623-
/// If `suffix` is empty, simply returns the original slice.
2627+
/// If `suffix` is empty, simply returns the original slice. If `suffix` is equal to the
2628+
/// original slice, returns an empty slice.
26242629
///
26252630
/// If the slice does not end with `suffix`, returns `None`.
26262631
///
@@ -2630,6 +2635,7 @@ impl<T> [T] {
26302635
/// let v = &[10, 40, 30];
26312636
/// assert_eq!(v.strip_suffix(&[30]), Some(&[10, 40][..]));
26322637
/// assert_eq!(v.strip_suffix(&[40, 30]), Some(&[10][..]));
2638+
/// assert_eq!(v.strip_suffix(&[10, 40, 30]), Some(&[][..]));
26332639
/// assert_eq!(v.strip_suffix(&[50]), None);
26342640
/// assert_eq!(v.strip_suffix(&[50, 30]), None);
26352641
/// ```

0 commit comments

Comments
 (0)