Skip to content

Commit 3946079

Browse files
Rollup merge of #39165 - frewsxcv:slice, r=GuillaumeGomez
A few improvements to the slice docs. * Simplify `Option::iter_mut` doc example. * Document 'empty' corner-cases for `slice::{starts_with, ends_with}`. * Indicate 'true' as code-like.
2 parents 87482ee + c8822da commit 3946079

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

src/libcollections/slice.rs

+24-10
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ impl<T> [T] {
181181
core_slice::SliceExt::len(self)
182182
}
183183

184-
/// Returns true if the slice has a length of 0.
184+
/// Returns `true` if the slice has a length of 0.
185185
///
186186
/// # Example
187187
///
@@ -549,12 +549,8 @@ impl<T> [T] {
549549
///
550550
/// ```
551551
/// let x = &mut [1, 2, 4];
552-
/// {
553-
/// let iterator = x.iter_mut();
554-
///
555-
/// for elem in iterator {
556-
/// *elem += 2;
557-
/// }
552+
/// for elem in x.iter_mut() {
553+
/// *elem += 2;
558554
/// }
559555
/// assert_eq!(x, &[3, 4, 6]);
560556
/// ```
@@ -889,7 +885,7 @@ impl<T> [T] {
889885
core_slice::SliceExt::rsplitn_mut(self, n, pred)
890886
}
891887

892-
/// Returns true if the slice contains an element with the given value.
888+
/// Returns `true` if the slice contains an element with the given value.
893889
///
894890
/// # Examples
895891
///
@@ -905,7 +901,7 @@ impl<T> [T] {
905901
core_slice::SliceExt::contains(self, x)
906902
}
907903

908-
/// Returns true if `needle` is a prefix of the slice.
904+
/// Returns `true` if `needle` is a prefix of the slice.
909905
///
910906
/// # Examples
911907
///
@@ -916,14 +912,23 @@ impl<T> [T] {
916912
/// assert!(!v.starts_with(&[50]));
917913
/// assert!(!v.starts_with(&[10, 50]));
918914
/// ```
915+
///
916+
/// Always returns `true` if `needle` is an empty slice:
917+
///
918+
/// ```
919+
/// let v = &[10, 40, 30];
920+
/// assert!(v.starts_with(&[]));
921+
/// let v: &[u8] = &[];
922+
/// assert!(v.starts_with(&[]));
923+
/// ```
919924
#[stable(feature = "rust1", since = "1.0.0")]
920925
pub fn starts_with(&self, needle: &[T]) -> bool
921926
where T: PartialEq
922927
{
923928
core_slice::SliceExt::starts_with(self, needle)
924929
}
925930

926-
/// Returns true if `needle` is a suffix of the slice.
931+
/// Returns `true` if `needle` is a suffix of the slice.
927932
///
928933
/// # Examples
929934
///
@@ -934,6 +939,15 @@ impl<T> [T] {
934939
/// assert!(!v.ends_with(&[50]));
935940
/// assert!(!v.ends_with(&[50, 30]));
936941
/// ```
942+
///
943+
/// Always returns `true` if `needle` is an empty slice:
944+
///
945+
/// ```
946+
/// let v = &[10, 40, 30];
947+
/// assert!(v.ends_with(&[]));
948+
/// let v: &[u8] = &[];
949+
/// assert!(v.ends_with(&[]));
950+
/// ```
937951
#[stable(feature = "rust1", since = "1.0.0")]
938952
pub fn ends_with(&self, needle: &[T]) -> bool
939953
where T: PartialEq

0 commit comments

Comments
 (0)