Skip to content

Commit f83f99c

Browse files
authored
Unrolled build for rust-lang#121343
Rollup merge of rust-lang#121343 - Takashiidobe:takashi/examples-for-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.
2 parents 381d699 + e59efe4 commit f83f99c

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

library/core/src/slice/mod.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ impl<T> [T] {
146146
/// ```
147147
/// let a = [1, 2, 3];
148148
/// assert!(!a.is_empty());
149+
///
150+
/// let b: &[i32] = &[];
151+
/// assert!(b.is_empty());
149152
/// ```
150153
#[stable(feature = "rust1", since = "1.0.0")]
151154
#[rustc_const_stable(feature = "const_slice_is_empty", since = "1.39.0")]
@@ -185,6 +188,9 @@ impl<T> [T] {
185188
/// *first = 5;
186189
/// }
187190
/// assert_eq!(x, &[5, 1, 2]);
191+
///
192+
/// let y: &mut [i32] = &mut [];
193+
/// assert_eq!(None, y.first_mut());
188194
/// ```
189195
#[stable(feature = "rust1", since = "1.0.0")]
190196
#[rustc_const_unstable(feature = "const_slice_first_last", issue = "83570")]
@@ -297,7 +303,7 @@ impl<T> [T] {
297303
if let [.., last] = self { Some(last) } else { None }
298304
}
299305

300-
/// Returns a mutable reference to the last item in the slice.
306+
/// Returns a mutable reference to the last item in the slice, or `None` if it is empty.
301307
///
302308
/// # Examples
303309
///
@@ -308,6 +314,9 @@ impl<T> [T] {
308314
/// *last = 10;
309315
/// }
310316
/// assert_eq!(x, &[0, 1, 10]);
317+
///
318+
/// let y: &mut [i32] = &mut [];
319+
/// assert_eq!(None, y.last_mut());
311320
/// ```
312321
#[stable(feature = "rust1", since = "1.0.0")]
313322
#[rustc_const_unstable(feature = "const_slice_first_last", issue = "83570")]

0 commit comments

Comments
 (0)