Skip to content

Commit 8a10015

Browse files
committed
Improve Option::inspect docs
1 parent c29082f commit 8a10015

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

library/core/src/option.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,18 +1074,22 @@ impl<T> Option<T> {
10741074
}
10751075
}
10761076

1077-
/// Calls the provided closure with a reference to the contained value (if [`Some`]).
1077+
/// Calls `f` with a reference to the contained value if [`Some`] and
1078+
/// returns the original Option.
10781079
///
10791080
/// # Examples
10801081
///
10811082
/// ```
1082-
/// let v = vec![1, 2, 3, 4, 5];
1083+
/// let list = vec![1, 2, 3];
10831084
///
1084-
/// // prints "got: 4"
1085-
/// 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");
10861090
///
10871091
/// // prints nothing
1088-
/// let x: Option<&usize> = v.get(5).inspect(|x| println!("got: {x}"));
1092+
/// list.get(5).inspect(|x| println!("got: {x}"));
10891093
/// ```
10901094
#[inline]
10911095
#[stable(feature = "result_option_inspect", since = "1.76.0")]

library/core/src/result.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,8 @@ 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 `f` with a reference to the contained value if the Result is
834+
/// [`Ok`] and returns the original Result.
834835
///
835836
/// # Examples
836837
///
@@ -851,7 +852,8 @@ impl<T, E> Result<T, E> {
851852
self
852853
}
853854

854-
/// Calls the provided closure with a reference to the contained error (if [`Err`]).
855+
/// Calls `f` with a reference to the contained value if the Result is
856+
/// [`Err`] and returns the original Result.
855857
///
856858
/// # Examples
857859
///

0 commit comments

Comments
 (0)