Skip to content

Add missing code examples on LocalKey #133498

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

Merged
merged 1 commit into from
Nov 28, 2024
Merged
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
30 changes: 28 additions & 2 deletions library/std/src/thread/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,19 @@ impl<T: 'static> LocalKey<T> {
/// This function will `panic!()` if the key currently has its
/// destructor running, and it **may** panic if the destructor has
/// previously been run for this thread.
///
/// # Examples
///
/// ```
/// thread_local! {
/// pub static STATIC: String = String::from("I am");
/// }
///
/// assert_eq!(
/// STATIC.with(|original_value| format!("{original_value} initialized")),
/// "I am initialized",
/// );
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn with<F, R>(&'static self, f: F) -> R
where
Expand All @@ -273,6 +286,19 @@ impl<T: 'static> LocalKey<T> {
///
/// This function will still `panic!()` if the key is uninitialized and the
/// key's initializer panics.
///
/// # Examples
///
/// ```
/// thread_local! {
/// pub static STATIC: String = String::from("I am");
/// }
///
/// assert_eq!(
/// STATIC.try_with(|original_value| format!("{original_value} initialized")),
/// Ok(String::from("I am initialized")),
/// );
/// ```
#[stable(feature = "thread_local_try_with", since = "1.26.0")]
#[inline]
pub fn try_with<F, R>(&'static self, f: F) -> Result<R, AccessError>
Expand Down Expand Up @@ -452,7 +478,7 @@ impl<T: 'static> LocalKey<RefCell<T>> {
/// Panics if the key currently has its destructor running,
/// and it **may** panic if the destructor has previously been run for this thread.
///
/// # Example
/// # Examples
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it's still only one example?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's how it's done in all std/core docs. I can't find where the rule is described though... Take a look at the String docs for example.

So even if there is only one code example, it's supposed to be titled "Examples".

///
/// ```
/// use std::cell::RefCell;
Expand Down Expand Up @@ -483,7 +509,7 @@ impl<T: 'static> LocalKey<RefCell<T>> {
/// Panics if the key currently has its destructor running,
/// and it **may** panic if the destructor has previously been run for this thread.
///
/// # Example
/// # Examples
///
/// ```
/// use std::cell::RefCell;
Expand Down
Loading