Skip to content

Commit c8822da

Browse files
committed
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.
1 parent c8af93f commit c8822da

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
///
@@ -540,12 +540,8 @@ impl<T> [T] {
540540
///
541541
/// ```
542542
/// let x = &mut [1, 2, 4];
543-
/// {
544-
/// let iterator = x.iter_mut();
545-
///
546-
/// for elem in iterator {
547-
/// *elem += 2;
548-
/// }
543+
/// for elem in x.iter_mut() {
544+
/// *elem += 2;
549545
/// }
550546
/// assert_eq!(x, &[3, 4, 6]);
551547
/// ```
@@ -880,7 +876,7 @@ impl<T> [T] {
880876
core_slice::SliceExt::rsplitn_mut(self, n, pred)
881877
}
882878

883-
/// Returns true if the slice contains an element with the given value.
879+
/// Returns `true` if the slice contains an element with the given value.
884880
///
885881
/// # Examples
886882
///
@@ -896,7 +892,7 @@ impl<T> [T] {
896892
core_slice::SliceExt::contains(self, x)
897893
}
898894

899-
/// Returns true if `needle` is a prefix of the slice.
895+
/// Returns `true` if `needle` is a prefix of the slice.
900896
///
901897
/// # Examples
902898
///
@@ -907,14 +903,23 @@ impl<T> [T] {
907903
/// assert!(!v.starts_with(&[50]));
908904
/// assert!(!v.starts_with(&[10, 50]));
909905
/// ```
906+
///
907+
/// Always returns `true` if `needle` is an empty slice:
908+
///
909+
/// ```
910+
/// let v = &[10, 40, 30];
911+
/// assert!(v.starts_with(&[]));
912+
/// let v: &[u8] = &[];
913+
/// assert!(v.starts_with(&[]));
914+
/// ```
910915
#[stable(feature = "rust1", since = "1.0.0")]
911916
pub fn starts_with(&self, needle: &[T]) -> bool
912917
where T: PartialEq
913918
{
914919
core_slice::SliceExt::starts_with(self, needle)
915920
}
916921

917-
/// Returns true if `needle` is a suffix of the slice.
922+
/// Returns `true` if `needle` is a suffix of the slice.
918923
///
919924
/// # Examples
920925
///
@@ -925,6 +930,15 @@ impl<T> [T] {
925930
/// assert!(!v.ends_with(&[50]));
926931
/// assert!(!v.ends_with(&[50, 30]));
927932
/// ```
933+
///
934+
/// Always returns `true` if `needle` is an empty slice:
935+
///
936+
/// ```
937+
/// let v = &[10, 40, 30];
938+
/// assert!(v.ends_with(&[]));
939+
/// let v: &[u8] = &[];
940+
/// assert!(v.ends_with(&[]));
941+
/// ```
928942
#[stable(feature = "rust1", since = "1.0.0")]
929943
pub fn ends_with(&self, needle: &[T]) -> bool
930944
where T: PartialEq

0 commit comments

Comments
 (0)