Skip to content

Commit 871357a

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 be1daa4 commit 871357a

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/libcollections/slice.rs

+17-8
Original file line numberDiff line numberDiff line change
@@ -342,15 +342,22 @@ impl<T> [T] {
342342
core_slice::SliceExt::last_mut(self)
343343
}
344344

345-
/// Returns the element of a slice at the given index, or `None` if the
346-
/// index is out of bounds.
345+
/// Returns a reference to an element or subslice depending on the type of
346+
/// index.
347+
///
348+
/// - If given a position, returns a reference to the element at that
349+
/// position or `None` if out of bounds.
350+
/// - If given a range, returns the subslice corresponding to that range,
351+
/// or `None` if out of bounds.
347352
///
348353
/// # Examples
349354
///
350355
/// ```
351356
/// let v = [10, 40, 30];
352357
/// assert_eq!(Some(&40), v.get(1));
358+
/// assert_eq!(Some(&[10, 40][..]), v.get(0..2));
353359
/// assert_eq!(None, v.get(3));
360+
/// assert_eq!(None, v.get(0..4));
354361
/// ```
355362
#[stable(feature = "rust1", since = "1.0.0")]
356363
#[inline]
@@ -360,7 +367,10 @@ impl<T> [T] {
360367
core_slice::SliceExt::get(self, index)
361368
}
362369

363-
/// Returns a mutable reference to the element at the given index.
370+
/// Returns a mutable reference to an element or subslice depending on the
371+
/// type of index (see [`get()`]) or `None` if the index is out of bounds.
372+
///
373+
/// [`get()`]: #method.get
364374
///
365375
/// # Examples
366376
///
@@ -372,7 +382,6 @@ impl<T> [T] {
372382
/// }
373383
/// assert_eq!(x, &[0, 42, 2]);
374384
/// ```
375-
/// or `None` if the index is out of bounds
376385
#[stable(feature = "rust1", since = "1.0.0")]
377386
#[inline]
378387
pub fn get_mut<I>(&mut self, index: I) -> Option<&mut I::Output>
@@ -381,8 +390,8 @@ impl<T> [T] {
381390
core_slice::SliceExt::get_mut(self, index)
382391
}
383392

384-
/// Returns a pointer to the element at the given index, without doing
385-
/// bounds checking. So use it very carefully!
393+
/// Returns a reference to an element or subslice, without doing bounds
394+
/// checking. So use it very carefully!
386395
///
387396
/// # Examples
388397
///
@@ -401,8 +410,8 @@ impl<T> [T] {
401410
core_slice::SliceExt::get_unchecked(self, index)
402411
}
403412

404-
/// Returns an unsafe mutable pointer to the element in index. So use it
405-
/// very carefully!
413+
/// Returns a mutable reference to an element or subslice, without doing
414+
/// bounds checking. So use it very carefully!
406415
///
407416
/// # Examples
408417
///

0 commit comments

Comments
 (0)