mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-16 00:43:50 +00:00
Rollup merge of #120815 - camsteffen:inspect-docs, r=m-ou-se
Improve `Option::inspect` docs * Refer to the function as "a function" instead of "the provided closure" since it is not necessarily a closure. * State that the original Option/Result is returned. * Adjust the example for `Option::inspect` to use chaining.
This commit is contained in:
commit
40f998d7e9
@ -1073,18 +1073,23 @@ impl<T> Option<T> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Calls the provided closure with a reference to the contained value (if [`Some`]).
|
||||
/// Calls a function with a reference to the contained value if [`Some`].
|
||||
///
|
||||
/// Returns the original option.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// let v = vec![1, 2, 3, 4, 5];
|
||||
/// let list = vec![1, 2, 3];
|
||||
///
|
||||
/// // prints "got: 4"
|
||||
/// let x: Option<&usize> = v.get(3).inspect(|x| println!("got: {x}"));
|
||||
/// // prints "got: 2"
|
||||
/// let x = list
|
||||
/// .get(1)
|
||||
/// .inspect(|x| println!("got: {x}"))
|
||||
/// .expect("list should be long enough");
|
||||
///
|
||||
/// // prints nothing
|
||||
/// let x: Option<&usize> = v.get(5).inspect(|x| println!("got: {x}"));
|
||||
/// list.get(5).inspect(|x| println!("got: {x}"));
|
||||
/// ```
|
||||
#[inline]
|
||||
#[stable(feature = "result_option_inspect", since = "1.76.0")]
|
||||
|
@ -830,7 +830,9 @@ impl<T, E> Result<T, E> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Calls the provided closure with a reference to the contained value (if [`Ok`]).
|
||||
/// Calls a function with a reference to the contained value if [`Ok`].
|
||||
///
|
||||
/// Returns the original result.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
@ -851,7 +853,9 @@ impl<T, E> Result<T, E> {
|
||||
self
|
||||
}
|
||||
|
||||
/// Calls the provided closure with a reference to the contained error (if [`Err`]).
|
||||
/// Calls a function with a reference to the contained value if [`Err`].
|
||||
///
|
||||
/// Returns the original result.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
|
Loading…
Reference in New Issue
Block a user