From 2c7bb2a1986865ac38adb5dee74e5ca5c980ec80 Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Wed, 7 Dec 2016 10:14:46 +0100 Subject: [PATCH] collections: update docs of slice get() and friends for the new SliceIndex trait. Also made the docs of the unchecked versions a bit clearer; they return a reference, not an "unsafe pointer". --- src/libcollections/slice.rs | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/libcollections/slice.rs b/src/libcollections/slice.rs index e615e780d2b5c..7694bf883a909 100644 --- a/src/libcollections/slice.rs +++ b/src/libcollections/slice.rs @@ -343,15 +343,24 @@ impl [T] { core_slice::SliceExt::last_mut(self) } - /// Returns the element of a slice at the given index, or `None` if the - /// index is out of bounds. + /// Returns a reference to an element or subslice depending on the type of + /// index. + /// + /// - If given a position, returns a reference to the element at that + /// position or [`None`] if out of bounds. + /// - If given a range, returns the subslice corresponding to that range, + /// or [`None`] if out of bounds. + /// + /// [`None`]: ../std/option/enum.Option.html#variant.None /// /// # Examples /// /// ``` /// let v = [10, 40, 30]; /// assert_eq!(Some(&40), v.get(1)); + /// assert_eq!(Some(&[10, 40][..]), v.get(0..2)); /// assert_eq!(None, v.get(3)); + /// assert_eq!(None, v.get(0..4)); /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] @@ -361,7 +370,11 @@ impl [T] { core_slice::SliceExt::get(self, index) } - /// Returns a mutable reference to the element at the given index. + /// Returns a mutable reference to an element or subslice depending on the + /// type of index (see [`get()`]) or [`None`] if the index is out of bounds. + /// + /// [`get()`]: #method.get + /// [`None`]: ../std/option/enum.Option.html#variant.None /// /// # Examples /// @@ -373,7 +386,6 @@ impl [T] { /// } /// assert_eq!(x, &[0, 42, 2]); /// ``` - /// or `None` if the index is out of bounds #[stable(feature = "rust1", since = "1.0.0")] #[inline] pub fn get_mut(&mut self, index: I) -> Option<&mut I::Output> @@ -382,8 +394,8 @@ impl [T] { core_slice::SliceExt::get_mut(self, index) } - /// Returns a pointer to the element at the given index, without doing - /// bounds checking. So use it very carefully! + /// Returns a reference to an element or subslice, without doing bounds + /// checking. So use it very carefully! /// /// # Examples /// @@ -402,8 +414,8 @@ impl [T] { core_slice::SliceExt::get_unchecked(self, index) } - /// Returns an unsafe mutable pointer to the element in index. So use it - /// very carefully! + /// Returns a mutable reference to an element or subslice, without doing + /// bounds checking. So use it very carefully! /// /// # Examples ///