Add docs about slicing slices at the ends · qinheping/verify-rust-std@b946b83 (original) (raw)

Original file line number Diff line number Diff line change
@@ -862,6 +862,27 @@ mod prim_array {}
862 862 /// assert_eq!(x, &[1, 7, 3]);
863 863 /// ```
864 864 ///
865 +/// It is possible to slice empty subranges of slices by using empty ranges (including `slice.len()..slice.len()`):
866 +/// ```
867 +/// let x = [1, 2, 3];
868 +/// let empty = &x[0..0]; // subslice before the first element
869 +/// assert_eq!(empty, &[]);
870 +/// let empty = &x[..0]; // same as &x[0..0]
871 +/// assert_eq!(empty, &[]);
872 +/// let empty = &x[1..1]; // empty subslice in the middle
873 +/// assert_eq!(empty, &[]);
874 +/// let empty = &x[3..3]; // subslice after the last element
875 +/// assert_eq!(empty, &[]);
876 +/// let empty = &x[3..]; // same as &x[3..3]
877 +/// assert_eq!(empty, &[]);
878 +/// ```
879 +///
880 +/// It is not allowed to use subranges that start with lower bound bigger than `slice.len()`:
881 +/// ```should_panic
882 +/// let x = vec![1, 2, 3];
883 +/// let _ = &x[4..4];
884 +/// ```
885 +///
865 886 /// As slices store the length of the sequence they refer to, they have twice
866 887 /// the size of pointers to [`Sized`](marker/trait.Sized.html) types.
867 888 /// Also see the reference on