File tree Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -862,6 +862,27 @@ mod prim_array {}
862862/// assert_eq!(x, &[1, 7, 3]);
863863/// ```
864864///
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+ ///
865886/// As slices store the length of the sequence they refer to, they have twice
866887/// the size of pointers to [`Sized`](marker/trait.Sized.html) types.
867888/// Also see the reference on
You can’t perform that action at this time.
0 commit comments