Skip to content

Commit

Permalink
collections: update docs of slice get() and friends
Browse files Browse the repository at this point in the history
for the new SliceIndex trait.  Also made the docs of the unchecked
versions a bit clearer; they return a reference, not an "unsafe
pointer".
  • Loading branch information
birkenfeld committed Jan 18, 2017
1 parent be1daa4 commit 871357a
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/libcollections/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,15 +342,22 @@ impl<T> [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.
///
/// # 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]
Expand All @@ -360,7 +367,10 @@ impl<T> [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
///
/// # Examples
///
Expand All @@ -372,7 +382,6 @@ impl<T> [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<I>(&mut self, index: I) -> Option<&mut I::Output>
Expand All @@ -381,8 +390,8 @@ impl<T> [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
///
Expand All @@ -401,8 +410,8 @@ impl<T> [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
///
Expand Down

0 comments on commit 871357a

Please sign in to comment.