Skip to content

Commit ce197e2

Browse files
authored
Rollup merge of #91346 - ibraheemdev:result-inspect, r=dtolnay
Add `Option::inspect` and `Result::{inspect, inspect_err}` ```rust // core::result impl Result<T, E> { pub fn inspect<F: FnOnce(&T)>(self, f: F) -> Self; pub fn inspect_err<F: FnOnce(&E)>(self, f: F) -> Self; } // core::option impl Option<T> { pub fn inspect<F: FnOnce(&T)>(self, f: F) -> Self; } ```
2 parents c09c16c + 2e8358e commit ce197e2

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

library/core/src/option.rs

+25
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,31 @@ impl<T> Option<T> {
848848
}
849849
}
850850

851+
/// Calls the provided closure with a reference to the contained value (if [`Some`]).
852+
///
853+
/// # Examples
854+
///
855+
/// ```
856+
/// #![feature(result_option_inspect)]
857+
///
858+
/// let v = vec![1, 2, 3, 4, 5];
859+
///
860+
/// // prints "got: 4"
861+
/// let x: Option<&usize> = v.get(3).inspect(|x| println!("got: {}", x));
862+
///
863+
/// // prints nothing
864+
/// let x: Option<&usize> = v.get(5).inspect(|x| println!("got: {}", x));
865+
/// ```
866+
#[inline]
867+
#[unstable(feature = "result_option_inspect", issue = "91345")]
868+
pub fn inspect<F: FnOnce(&T)>(self, f: F) -> Self {
869+
if let Some(ref x) = self {
870+
f(x);
871+
}
872+
873+
self
874+
}
875+
851876
/// Returns the provided default result (if none),
852877
/// or applies a function to the contained value (if any).
853878
///

library/core/src/result.rs

+47
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,53 @@ impl<T, E> Result<T, E> {
854854
}
855855
}
856856

857+
/// Calls the provided closure with a reference to the contained value (if [`Ok`]).
858+
///
859+
/// # Examples
860+
///
861+
/// ```
862+
/// #![feature(result_option_inspect)]
863+
///
864+
/// let x: u8 = "4"
865+
/// .parse::<u8>()
866+
/// .inspect(|x| println!("original: {}", x))
867+
/// .map(|x| x.pow(3))
868+
/// .expect("failed to parse number");
869+
/// ```
870+
#[inline]
871+
#[unstable(feature = "result_option_inspect", issue = "91345")]
872+
pub fn inspect<F: FnOnce(&T)>(self, f: F) -> Self {
873+
if let Ok(ref t) = self {
874+
f(t);
875+
}
876+
877+
self
878+
}
879+
880+
/// Calls the provided closure with a reference to the contained error (if [`Err`]).
881+
///
882+
/// # Examples
883+
///
884+
/// ```
885+
/// #![feature(result_option_inspect)]
886+
///
887+
/// use std::{fs, io};
888+
///
889+
/// fn read() -> io::Result<String> {
890+
/// fs::read_to_string("address.txt")
891+
/// .inspect_err(|e| eprintln!("failed to read file: {}", e))
892+
/// }
893+
/// ```
894+
#[inline]
895+
#[unstable(feature = "result_option_inspect", issue = "91345")]
896+
pub fn inspect_err<F: FnOnce(&E)>(self, f: F) -> Self {
897+
if let Err(ref e) = self {
898+
f(e);
899+
}
900+
901+
self
902+
}
903+
857904
/////////////////////////////////////////////////////////////////////////
858905
// Iterator constructors
859906
/////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)