@@ -181,7 +181,7 @@ impl<T> [T] {
181
181
core_slice:: SliceExt :: len ( self )
182
182
}
183
183
184
- /// Returns true if the slice has a length of 0.
184
+ /// Returns ` true` if the slice has a length of 0.
185
185
///
186
186
/// # Example
187
187
///
@@ -342,15 +342,22 @@ impl<T> [T] {
342
342
core_slice:: SliceExt :: last_mut ( self )
343
343
}
344
344
345
- /// Returns the element of a slice at the given index, or `None` if the
346
- /// index is out of bounds.
345
+ /// Returns a reference to an element or subslice depending on the type of
346
+ /// index.
347
+ ///
348
+ /// - If given a position, returns a reference to the element at that
349
+ /// position or `None` if out of bounds.
350
+ /// - If given a range, returns the subslice corresponding to that range,
351
+ /// or `None` if out of bounds.
347
352
///
348
353
/// # Examples
349
354
///
350
355
/// ```
351
356
/// let v = [10, 40, 30];
352
357
/// assert_eq!(Some(&40), v.get(1));
358
+ /// assert_eq!(Some(&[10, 40][..]), v.get(0..2));
353
359
/// assert_eq!(None, v.get(3));
360
+ /// assert_eq!(None, v.get(0..4));
354
361
/// ```
355
362
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
356
363
#[ inline]
@@ -360,7 +367,10 @@ impl<T> [T] {
360
367
core_slice:: SliceExt :: get ( self , index)
361
368
}
362
369
363
- /// Returns a mutable reference to the element at the given index.
370
+ /// Returns a mutable reference to an element or subslice depending on the
371
+ /// type of index (see [`get()`]) or `None` if the index is out of bounds.
372
+ ///
373
+ /// [`get()`]: #method.get
364
374
///
365
375
/// # Examples
366
376
///
@@ -372,7 +382,6 @@ impl<T> [T] {
372
382
/// }
373
383
/// assert_eq!(x, &[0, 42, 2]);
374
384
/// ```
375
- /// or `None` if the index is out of bounds
376
385
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
377
386
#[ inline]
378
387
pub fn get_mut < I > ( & mut self , index : I ) -> Option < & mut I :: Output >
@@ -381,8 +390,8 @@ impl<T> [T] {
381
390
core_slice:: SliceExt :: get_mut ( self , index)
382
391
}
383
392
384
- /// Returns a pointer to the element at the given index , without doing
385
- /// bounds checking. So use it very carefully!
393
+ /// Returns a reference to an element or subslice , without doing bounds
394
+ /// checking. So use it very carefully!
386
395
///
387
396
/// # Examples
388
397
///
@@ -401,8 +410,8 @@ impl<T> [T] {
401
410
core_slice:: SliceExt :: get_unchecked ( self , index)
402
411
}
403
412
404
- /// Returns an unsafe mutable pointer to the element in index. So use it
405
- /// very carefully!
413
+ /// Returns a mutable reference to an element or subslice, without doing
414
+ /// bounds checking. So use it very carefully!
406
415
///
407
416
/// # Examples
408
417
///
@@ -540,12 +549,8 @@ impl<T> [T] {
540
549
///
541
550
/// ```
542
551
/// let x = &mut [1, 2, 4];
543
- /// {
544
- /// let iterator = x.iter_mut();
545
- ///
546
- /// for elem in iterator {
547
- /// *elem += 2;
548
- /// }
552
+ /// for elem in x.iter_mut() {
553
+ /// *elem += 2;
549
554
/// }
550
555
/// assert_eq!(x, &[3, 4, 6]);
551
556
/// ```
@@ -880,7 +885,7 @@ impl<T> [T] {
880
885
core_slice:: SliceExt :: rsplitn_mut ( self , n, pred)
881
886
}
882
887
883
- /// 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.
884
889
///
885
890
/// # Examples
886
891
///
@@ -896,7 +901,7 @@ impl<T> [T] {
896
901
core_slice:: SliceExt :: contains ( self , x)
897
902
}
898
903
899
- /// Returns true if `needle` is a prefix of the slice.
904
+ /// Returns ` true` if `needle` is a prefix of the slice.
900
905
///
901
906
/// # Examples
902
907
///
@@ -907,14 +912,23 @@ impl<T> [T] {
907
912
/// assert!(!v.starts_with(&[50]));
908
913
/// assert!(!v.starts_with(&[10, 50]));
909
914
/// ```
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
+ /// ```
910
924
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
911
925
pub fn starts_with ( & self , needle : & [ T ] ) -> bool
912
926
where T : PartialEq
913
927
{
914
928
core_slice:: SliceExt :: starts_with ( self , needle)
915
929
}
916
930
917
- /// Returns true if `needle` is a suffix of the slice.
931
+ /// Returns ` true` if `needle` is a suffix of the slice.
918
932
///
919
933
/// # Examples
920
934
///
@@ -925,6 +939,15 @@ impl<T> [T] {
925
939
/// assert!(!v.ends_with(&[50]));
926
940
/// assert!(!v.ends_with(&[50, 30]));
927
941
/// ```
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
+ /// ```
928
951
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
929
952
pub fn ends_with ( & self , needle : & [ T ] ) -> bool
930
953
where T : PartialEq
0 commit comments