More practical examples for Result::and_then

This commit is contained in:
cyqsimon 2022-02-10 17:59:46 +08:00
parent 73a5f01263
commit bd421e2880
No known key found for this signature in database
GPG Key ID: 1D8CE2F297390D65

View File

@ -1281,16 +1281,22 @@ impl<T, E> Result<T, E> {
///
/// # Examples
///
/// Basic usage:
/// ```
/// fn squared_string(x: u32) -> Result<String, &'static str> {
/// Ok((x * x).to_string())
/// }
///
/// assert_eq!(Ok(2).and_then(squared_string), Ok(4.to_string()));
/// assert_eq!(Err("not a number").and_then(squared_string), Err("not a number"));
/// ```
///
/// Often used to chain fallible operations that may return [`Err`].
///
/// ```
/// fn sq(x: u32) -> Result<u32, u32> { Ok(x * x) }
/// fn err(x: u32) -> Result<u32, u32> { Err(x) }
/// use std::path::Path;
///
/// assert_eq!(Ok(2).and_then(sq).and_then(sq), Ok(16));
/// assert_eq!(Ok(2).and_then(sq).and_then(err), Err(4));
/// assert_eq!(Ok(2).and_then(err).and_then(sq), Err(2));
/// assert_eq!(Err(3).and_then(sq).and_then(sq), Err(3));
/// let root_modified_time = Path::new("/").metadata().and_then(|md| md.modified());
/// assert!(root_modified_time.is_ok())
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]