about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAngelicosPhosphoros <angelicos.phosphoros@protonmail.com>2024-10-08 00:23:53 +0200
committerAngelicosPhosphoros <angelicos.phosphoros@protonmail.com>2024-10-08 00:23:53 +0200
commitcb267b4c560715f9e5fa60010c92be4003655c67 (patch)
treeb151c143c7876172ff570b0ff2a9ab8909147780
parentbaaf3e65abab5a281715230bcb1785610629ae7d (diff)
downloadrust-cb267b4c560715f9e5fa60010c92be4003655c67.tar.gz
rust-cb267b4c560715f9e5fa60010c92be4003655c67.zip
Add docs about slicing slices at the ends
Closes https://github.com/rust-lang/rust/issues/60783
-rw-r--r--library/core/src/primitive_docs.rs21
1 files changed, 21 insertions, 0 deletions
diff --git a/library/core/src/primitive_docs.rs b/library/core/src/primitive_docs.rs
index c25501f1200..89936dc12ac 100644
--- a/library/core/src/primitive_docs.rs
+++ b/library/core/src/primitive_docs.rs
@@ -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