Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Option::inspect docs #120815

Merged
merged 1 commit into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions library/core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
/// ```
/// let v = vec![1, 2, 3, 4, 5];
/// let list = vec![1, 2, 3];
///
/// // prints "got: 4"
/// let x: Option<&usize> = v.get(3).inspect(|x| println!("got: {x}"));
/// // prints "got: 2"
/// let x = list
/// .get(1)
/// .inspect(|x| println!("got: {x}"))
/// .expect("list should be long enough");
///
/// // prints nothing
/// let x: Option<&usize> = v.get(5).inspect(|x| println!("got: {x}"));
/// list.get(5).inspect(|x| println!("got: {x}"));
/// ```
#[inline]
#[stable(feature = "result_option_inspect", since = "1.76.0")]
Expand Down
8 changes: 6 additions & 2 deletions library/core/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand All @@ -851,7 +853,9 @@ impl<T, E> Result<T, E> {
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
///
Expand Down
Loading