Improve Option::inspect docs

This commit is contained in:
Cameron Steffen 2024-02-08 16:41:48 -06:00
parent 8fb67fb37f
commit e9059cb8aa
2 changed files with 16 additions and 7 deletions

View File

@ -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 /// # Examples
/// ///
/// ``` /// ```
/// let v = vec![1, 2, 3, 4, 5]; /// let list = vec![1, 2, 3];
/// ///
/// // prints "got: 4" /// // prints "got: 2"
/// let x: Option<&usize> = v.get(3).inspect(|x| println!("got: {x}")); /// let x = list
/// .get(1)
/// .inspect(|x| println!("got: {x}"))
/// .expect("list should be long enough");
/// ///
/// // prints nothing /// // prints nothing
/// let x: Option<&usize> = v.get(5).inspect(|x| println!("got: {x}")); /// list.get(5).inspect(|x| println!("got: {x}"));
/// ``` /// ```
#[inline] #[inline]
#[stable(feature = "result_option_inspect", since = "1.76.0")] #[stable(feature = "result_option_inspect", since = "1.76.0")]

View File

@ -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 /// # Examples
/// ///
@ -851,7 +853,9 @@ impl<T, E> Result<T, E> {
self 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 /// # Examples
/// ///