Skip to content

Clarify behavior of slice prefix/suffix operations in case of equality #121634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions library/core/src/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2519,14 +2519,15 @@ impl<T> [T] {
cmp::SliceContains::slice_contains(x, self)
}

/// Returns `true` if `needle` is a prefix of the slice.
/// Returns `true` if `needle` is a prefix of the slice or equal to the slice.
///
/// # Examples
///
/// ```
/// let v = [10, 40, 30];
/// assert!(v.starts_with(&[10]));
/// assert!(v.starts_with(&[10, 40]));
/// assert!(v.starts_with(&v));
/// assert!(!v.starts_with(&[50]));
/// assert!(!v.starts_with(&[10, 50]));
/// ```
Expand All @@ -2549,14 +2550,15 @@ impl<T> [T] {
self.len() >= n && needle == &self[..n]
}

/// Returns `true` if `needle` is a suffix of the slice.
/// Returns `true` if `needle` is a suffix of the slice or equal to the slice.
///
/// # Examples
///
/// ```
/// let v = [10, 40, 30];
/// assert!(v.ends_with(&[30]));
/// assert!(v.ends_with(&[40, 30]));
/// assert!(v.ends_with(&v));
/// assert!(!v.ends_with(&[50]));
/// assert!(!v.ends_with(&[50, 30]));
/// ```
Expand All @@ -2582,7 +2584,8 @@ impl<T> [T] {
/// Returns a subslice with the prefix removed.
///
/// If the slice starts with `prefix`, returns the subslice after the prefix, wrapped in `Some`.
/// If `prefix` is empty, simply returns the original slice.
/// If `prefix` is empty, simply returns the original slice. If `prefix` is equal to the
/// original slice, returns an empty slice.
///
/// If the slice does not start with `prefix`, returns `None`.
///
Expand All @@ -2592,6 +2595,7 @@ impl<T> [T] {
/// let v = &[10, 40, 30];
/// assert_eq!(v.strip_prefix(&[10]), Some(&[40, 30][..]));
/// assert_eq!(v.strip_prefix(&[10, 40]), Some(&[30][..]));
/// assert_eq!(v.strip_prefix(&[10, 40, 30]), Some(&[][..]));
/// assert_eq!(v.strip_prefix(&[50]), None);
/// assert_eq!(v.strip_prefix(&[10, 50]), None);
///
Expand Down Expand Up @@ -2620,7 +2624,8 @@ impl<T> [T] {
/// Returns a subslice with the suffix removed.
///
/// If the slice ends with `suffix`, returns the subslice before the suffix, wrapped in `Some`.
/// If `suffix` is empty, simply returns the original slice.
/// If `suffix` is empty, simply returns the original slice. If `suffix` is equal to the
/// original slice, returns an empty slice.
///
/// If the slice does not end with `suffix`, returns `None`.
///
Expand All @@ -2630,6 +2635,7 @@ impl<T> [T] {
/// let v = &[10, 40, 30];
/// assert_eq!(v.strip_suffix(&[30]), Some(&[10, 40][..]));
/// assert_eq!(v.strip_suffix(&[40, 30]), Some(&[10][..]));
/// assert_eq!(v.strip_suffix(&[10, 40, 30]), Some(&[][..]));
/// assert_eq!(v.strip_suffix(&[50]), None);
/// assert_eq!(v.strip_suffix(&[50, 30]), None);
/// ```
Expand Down