Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add docs about slicing slices at the ends #131383

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions library/core/src/primitive_docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,27 @@ mod prim_array {}
/// assert_eq!(x, &[1, 7, 3]);
/// ```
///
/// It is possible to slice empty subranges of slices by using empty ranges (including `slice.len()..slice.len()`):
/// ```
/// let x = [1, 2, 3];
/// let empty = &x[0..0]; // subslice before the first element
/// assert_eq!(empty, &[]);
/// let empty = &x[..0]; // same as &x[0..0]
/// assert_eq!(empty, &[]);
/// let empty = &x[1..1]; // empty subslice in the middle
/// assert_eq!(empty, &[]);
/// let empty = &x[3..3]; // subslice after the last element
/// assert_eq!(empty, &[]);
/// let empty = &x[3..]; // same as &x[3..3]
/// assert_eq!(empty, &[]);
/// ```
///
/// It is not allowed to use subranges that start with lower bound bigger than `slice.len()`:
/// ```should_panic
/// let x = vec![1, 2, 3];
/// let _ = &x[4..4];
/// ```
///
/// As slices store the length of the sequence they refer to, they have twice
/// the size of pointers to [`Sized`](marker/trait.Sized.html) types.
/// Also see the reference on
Expand Down
Loading