Skip to content

Commit

Permalink
Rollup merge of rust-lang#121343 - Takashiidobe:takashi/examples-for-…
Browse files Browse the repository at this point in the history
…slice, r=Mark-Simulacrum

Add examples for some methods on slices

Adds some examples to some methods on slice.

`is_empty` didn't have an example for an empty slice, even though `str` and the collections all have one, so I added that in.

`first_mut` and `last_mut` didn't have an example for what happens when the slice is empty, whereas `first` and `last` do, so I added that too.
  • Loading branch information
matthiaskrgr authored Feb 24, 2024
2 parents a597352 + e59efe4 commit 6b2c470
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion library/core/src/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ impl<T> [T] {
/// ```
/// let a = [1, 2, 3];
/// assert!(!a.is_empty());
///
/// let b: &[i32] = &[];
/// assert!(b.is_empty());
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_stable(feature = "const_slice_is_empty", since = "1.39.0")]
Expand Down Expand Up @@ -185,6 +188,9 @@ impl<T> [T] {
/// *first = 5;
/// }
/// assert_eq!(x, &[5, 1, 2]);
///
/// let y: &mut [i32] = &mut [];
/// assert_eq!(None, y.first_mut());
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_slice_first_last", issue = "83570")]
Expand Down Expand Up @@ -297,7 +303,7 @@ impl<T> [T] {
if let [.., last] = self { Some(last) } else { None }
}

/// Returns a mutable reference to the last item in the slice.
/// Returns a mutable reference to the last item in the slice, or `None` if it is empty.
///
/// # Examples
///
Expand All @@ -308,6 +314,9 @@ impl<T> [T] {
/// *last = 10;
/// }
/// assert_eq!(x, &[0, 1, 10]);
///
/// let y: &mut [i32] = &mut [];
/// assert_eq!(None, y.last_mut());
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_slice_first_last", issue = "83570")]
Expand Down

0 comments on commit 6b2c470

Please sign in to comment.