From bfffe6d9d25d52f75705b5ebcc741b6844c2aa81 Mon Sep 17 00:00:00 2001 From: Ulrik Sverdrup Date: Thu, 10 Mar 2016 21:36:43 +0100 Subject: [PATCH] Clarify doc for slice slicing (Index impls) This is a follow up for PR #32099 and #32057 --- src/libcore/slice.rs | 58 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs index 8acd0c8f2cf06..823acf68001c7 100644 --- a/src/libcore/slice.rs +++ b/src/libcore/slice.rs @@ -535,6 +535,16 @@ fn slice_index_order_fail(index: usize, end: usize) -> ! { // FIXME implement indexing with inclusive ranges +/// Implements slicing with syntax `&self[begin .. end]`. +/// +/// Returns a slice of self for the index range [`begin`..`end`). +/// +/// This operation is `O(1)`. +/// +/// # Panics +/// +/// Requires that `begin <= end` and `end <= self.len()`, +/// otherwise slicing will panic. #[stable(feature = "rust1", since = "1.0.0")] impl ops::Index> for [T] { type Output = [T]; @@ -554,6 +564,13 @@ impl ops::Index> for [T] { } } } + +/// Implements slicing with syntax `&self[.. end]`. +/// +/// Returns a slice of self from the beginning until but not including +/// the index `end`. +/// +/// Equivalent to `&self[0 .. end]` #[stable(feature = "rust1", since = "1.0.0")] impl ops::Index> for [T] { type Output = [T]; @@ -563,6 +580,12 @@ impl ops::Index> for [T] { self.index(0 .. index.end) } } + +/// Implements slicing with syntax `&self[begin ..]`. +/// +/// Returns a slice of self from and including the index `begin` until the end. +/// +/// Equivalent to `&self[begin .. self.len()]` #[stable(feature = "rust1", since = "1.0.0")] impl ops::Index> for [T] { type Output = [T]; @@ -572,6 +595,12 @@ impl ops::Index> for [T] { self.index(index.start .. self.len()) } } + +/// Implements slicing with syntax `&self[..]`. +/// +/// Returns a slice of the whole slice. This operation can not panic. +/// +/// Equivalent to `&self[0 .. self.len()]` #[stable(feature = "rust1", since = "1.0.0")] impl ops::Index for [T] { type Output = [T]; @@ -608,6 +637,16 @@ impl ops::Index> for [T] { } } +/// Implements mutable slicing with syntax `&mut self[begin .. end]`. +/// +/// Returns a slice of self for the index range [`begin`..`end`). +/// +/// This operation is `O(1)`. +/// +/// # Panics +/// +/// Requires that `begin <= end` and `end <= self.len()`, +/// otherwise slicing will panic. #[stable(feature = "rust1", since = "1.0.0")] impl ops::IndexMut> for [T] { #[inline] @@ -625,6 +664,13 @@ impl ops::IndexMut> for [T] { } } } + +/// Implements mutable slicing with syntax `&mut self[.. end]`. +/// +/// Returns a slice of self from the beginning until but not including +/// the index `end`. +/// +/// Equivalent to `&mut self[0 .. end]` #[stable(feature = "rust1", since = "1.0.0")] impl ops::IndexMut> for [T] { #[inline] @@ -632,6 +678,12 @@ impl ops::IndexMut> for [T] { self.index_mut(0 .. index.end) } } + +/// Implements mutable slicing with syntax `&mut self[begin ..]`. +/// +/// Returns a slice of self from and including the index `begin` until the end. +/// +/// Equivalent to `&mut self[begin .. self.len()]` #[stable(feature = "rust1", since = "1.0.0")] impl ops::IndexMut> for [T] { #[inline] @@ -640,6 +692,12 @@ impl ops::IndexMut> for [T] { self.index_mut(index.start .. len) } } + +/// Implements mutable slicing with syntax `&mut self[..]`. +/// +/// Returns a slice of the whole slice. This operation can not panic. +/// +/// Equivalent to `&mut self[0 .. self.len()]` #[stable(feature = "rust1", since = "1.0.0")] impl ops::IndexMut for [T] { #[inline]