Skip to content
Closed
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
28 changes: 20 additions & 8 deletions src/libcollections/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,15 +343,24 @@ 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.
///
/// [`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]
Expand All @@ -361,7 +370,11 @@ 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
/// [`None`]: ../std/option/enum.Option.html#variant.None
///
/// # Examples
///
Expand All @@ -373,7 +386,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 @@ -382,8 +394,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 @@ -402,8 +414,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