Skip to content

Commit 2c7bb2a

Browse files
committed
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".
1 parent 02ea82d commit 2c7bb2a

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

src/libcollections/slice.rs

+20-8
Original file line numberDiff line numberDiff line change
@@ -343,15 +343,24 @@ impl<T> [T] {
343343
core_slice::SliceExt::last_mut(self)
344344
}
345345

346-
/// Returns the element of a slice at the given index, or `None` if the
347-
/// index is out of bounds.
346+
/// Returns a reference to an element or subslice depending on the type of
347+
/// index.
348+
///
349+
/// - If given a position, returns a reference to the element at that
350+
/// position or [`None`] if out of bounds.
351+
/// - If given a range, returns the subslice corresponding to that range,
352+
/// or [`None`] if out of bounds.
353+
///
354+
/// [`None`]: ../std/option/enum.Option.html#variant.None
348355
///
349356
/// # Examples
350357
///
351358
/// ```
352359
/// let v = [10, 40, 30];
353360
/// assert_eq!(Some(&40), v.get(1));
361+
/// assert_eq!(Some(&[10, 40][..]), v.get(0..2));
354362
/// assert_eq!(None, v.get(3));
363+
/// assert_eq!(None, v.get(0..4));
355364
/// ```
356365
#[stable(feature = "rust1", since = "1.0.0")]
357366
#[inline]
@@ -361,7 +370,11 @@ impl<T> [T] {
361370
core_slice::SliceExt::get(self, index)
362371
}
363372

364-
/// Returns a mutable reference to the element at the given index.
373+
/// Returns a mutable reference to an element or subslice depending on the
374+
/// type of index (see [`get()`]) or [`None`] if the index is out of bounds.
375+
///
376+
/// [`get()`]: #method.get
377+
/// [`None`]: ../std/option/enum.Option.html#variant.None
365378
///
366379
/// # Examples
367380
///
@@ -373,7 +386,6 @@ impl<T> [T] {
373386
/// }
374387
/// assert_eq!(x, &[0, 42, 2]);
375388
/// ```
376-
/// or `None` if the index is out of bounds
377389
#[stable(feature = "rust1", since = "1.0.0")]
378390
#[inline]
379391
pub fn get_mut<I>(&mut self, index: I) -> Option<&mut I::Output>
@@ -382,8 +394,8 @@ impl<T> [T] {
382394
core_slice::SliceExt::get_mut(self, index)
383395
}
384396

385-
/// Returns a pointer to the element at the given index, without doing
386-
/// bounds checking. So use it very carefully!
397+
/// Returns a reference to an element or subslice, without doing bounds
398+
/// checking. So use it very carefully!
387399
///
388400
/// # Examples
389401
///
@@ -402,8 +414,8 @@ impl<T> [T] {
402414
core_slice::SliceExt::get_unchecked(self, index)
403415
}
404416

405-
/// Returns an unsafe mutable pointer to the element in index. So use it
406-
/// very carefully!
417+
/// Returns a mutable reference to an element or subslice, without doing
418+
/// bounds checking. So use it very carefully!
407419
///
408420
/// # Examples
409421
///

0 commit comments

Comments
 (0)