Skip to content

Commit 40f998d

Browse files
authored
Rollup merge of rust-lang#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.
2 parents 5250aba + e9059cb commit 40f998d

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

library/core/src/option.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -1073,18 +1073,23 @@ impl<T> Option<T> {
10731073
}
10741074
}
10751075

1076-
/// Calls the provided closure with a reference to the contained value (if [`Some`]).
1076+
/// Calls a function with a reference to the contained value if [`Some`].
1077+
///
1078+
/// Returns the original option.
10771079
///
10781080
/// # Examples
10791081
///
10801082
/// ```
1081-
/// let v = vec![1, 2, 3, 4, 5];
1083+
/// let list = vec![1, 2, 3];
10821084
///
1083-
/// // prints "got: 4"
1084-
/// let x: Option<&usize> = v.get(3).inspect(|x| println!("got: {x}"));
1085+
/// // prints "got: 2"
1086+
/// let x = list
1087+
/// .get(1)
1088+
/// .inspect(|x| println!("got: {x}"))
1089+
/// .expect("list should be long enough");
10851090
///
10861091
/// // prints nothing
1087-
/// let x: Option<&usize> = v.get(5).inspect(|x| println!("got: {x}"));
1092+
/// list.get(5).inspect(|x| println!("got: {x}"));
10881093
/// ```
10891094
#[inline]
10901095
#[stable(feature = "result_option_inspect", since = "1.76.0")]

library/core/src/result.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,9 @@ impl<T, E> Result<T, E> {
830830
}
831831
}
832832

833-
/// Calls the provided closure with a reference to the contained value (if [`Ok`]).
833+
/// Calls a function with a reference to the contained value if [`Ok`].
834+
///
835+
/// Returns the original result.
834836
///
835837
/// # Examples
836838
///
@@ -851,7 +853,9 @@ impl<T, E> Result<T, E> {
851853
self
852854
}
853855

854-
/// Calls the provided closure with a reference to the contained error (if [`Err`]).
856+
/// Calls a function with a reference to the contained value if [`Err`].
857+
///
858+
/// Returns the original result.
855859
///
856860
/// # Examples
857861
///

0 commit comments

Comments
 (0)