8000 Add doc for `NonZero*` const creation by cptpiepmatz · Pull Request #146924 · rust-lang/rust · GitHub
[go: up one dir, main page]

Skip to content

Conversation

cptpiepmatz
Copy link
Contributor

I ran into trouble using NonZero* values because I didn’t see any clear way to create them at compile time. At first I ended up using NonZero*::new_unchecked a lot, until I realized that Option::unwrap and Option::expect are const and can be used in a const context. With that, you can create non-zero values at compile time safely, without touching unsafe. This wasn’t obvious to me and my peers who’ve been using Rust for a while, so I thought adding a note to the docs would make it easier for others to discover.

If this should be worded differently or placed in another location, we can do that. I just want to make this more obvious.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Sep 23, 2025
@rustbot
Copy link
Collaborator
rustbot commented Sep 23, 2025

r? @ibraheemdev

rustbot has assigned @ibraheemdev.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@hkBst
Copy link
Member
hkBst commented Sep 23, 2025

Since this issue is not restricted to NonZero, but more generally applies to const functions returning an Option or Result, another idea is to add such information instead to https://doc.rust-lang.org/nightly/std/keyword.const.html#compile-time-constants.

@cptpiepmatz
Copy link
Contributor Author

Since this issue is not restricted to NonZero, but more generally applies to const functions returning an Option or Result, another idea is to add such information instead to doc.rust-lang.org/nightly/std/keyword.const.html#compile-time-constants.

True but I did not come across this issue anywhere besides non-zero values really.

/// ```
#[doc = concat!("use std::num::", stringify!($Ty), ";")]
///
#[doc = concat!("const TEN: ", stringify!($Ty), " = ", stringify!($Ty) , r#"::new(10).expect("ten is non-zero");"#)]
Copy link
Member

Choose a reason for hiding this comment

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

How about using an inline-const-block here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So what @clubby789 asked to remove? #146924 (comment)

Copy link
Member
@joboet joboet Sep 24, 2025

Choose a reason for hiding this comment

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

No, but the whole const TEN item:

Suggested change
#[doc = concat!("const TEN: ", stringify!($Ty), " = ", stringify!($Ty) , r#"::new(10).expect("ten is non-zero");"#)]
#[doc = concat!("let ten = const { ", stringify!($Ty) , r#"::new(10).expect("ten is non-zero") };"#)]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think both are fine. Why should we prefer this one?

Copy link
Contributor

Choose a reason for hiding this comment

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

I prefer the const TEN: - we're probably 8000 documenting this to someone relatively new to the language, and a global const variable is more familiar/conceptually simpler than an inline const block imo

Copy link
Member
@hkBst hkBst Sep 25, 2025

Choose a reason for hiding this comment

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

We can have both. const X for https://doc.rust-lang.org/nightly/std/keyword.const.html#compile-time-constants and const {} for https://doc.rust-lang.org/nightly/std/keyword.const.html#compile-time-blocks. (Or do both here, but I've seen no evidence that NonZero is the dominant pattern for this.)

Copy link
Member

Choose a reason for hiding this comment

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

we're probably documenting this to someone relatively new to the language

That's mostly why I advocated for const-blocks – I'd like more people to be aware of them. But I don't feel strongly, the current version is fine.

@joboet joboet assigned joboet and unassigned ibraheemdev Sep 24, 2025
@joboet
Copy link
Member
joboet commented Sep 25, 2025

Please squash commits and then r=me.
@bors delegate+
@bors rollup

@bors
Copy link
Collaborator
bors commented Sep 25, 2025

✌️ @cptpiepmatz, you can now approve this pull request!

If @joboet told you to "r=me" after making some further change, please make that change, then do @bors r=@joboet

@cptpiepmatz cptpiepmatz force-pushed the doc-nonzero-const-creation branch from 55487c1 to 185ae69 Compare September 25, 2025 15:54
@cptpiepmatz
Copy link
Contributor Author

@bors r=@joboet

I hope I did that right

@bors
Copy link
Collaborator
bors commented Sep 25, 2025

📌 Commit 185ae69 has been approved by joboet

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 25, 2025
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Sep 25, 2025
…tion, r=joboet

Add doc for `NonZero*` const creation

I ran into trouble using `NonZero*` values because I didn’t see any clear way to create them at compile time. At first I ended up using `NonZero*::new_unchecked` a lot, until I realized that `Option::unwrap` and `Option::expect` are `const` and can be used in a `const` context. With that, you can create non-zero values at compile time safely, without touching `unsafe`. This wasn’t obvious to me and my peers who’ve been using Rust for a while, so I thought adding a note to the docs would make it easier for others to discover.

If this should be worded differently or placed in another location, we can do that. I just want to make this more obvious.
bors added a commit that referenced this pull request Sep 25, 2025
Rollup of 8 pull requests

Successful merges:

 - #116882 (rustdoc: hide `#[repr]` if it isn't part of the public ABI)
 - #135771 ([rustdoc] Add support for associated items in "jump to def" feature)
 - #141032 (avoid violating `slice::from_raw_parts` safety contract in `Vec::extract_if`)
 - #142401 (Add proper name mangling for pattern types)
 - #146293 (feat: non-panicking `Vec::try_remove`)
 - #146859 (BTreeMap: Don't leak allocators when initializing nodes)
 - #146924 (Add doc for `NonZero*` const creation)
 - #146933 (Make `render_example_with_highlighting` return an `impl fmt::Display`)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 781f71a into rust-lang:master Sep 25, 2025
10 checks passed
@rustbot rustbot added this to the 1.92.0 milestone Sep 25, 2025
rust-timer added a commit that referenced this pull request Sep 25, 2025
Rollup merge of #146924 - cptpiepmatz:doc-nonzero-const-creation, r=joboet

Add doc for `NonZero*` const creation

I ran into trouble using `NonZero*` values because I didn’t see any clear way to create them at compile time. At first I ended up using `NonZero*::new_unchecked` a lot, until I realized that `Option::unwrap` and `Option::expect` are `const` and can be used in a `const` context. With that, you can create non-zero values at compile time safely, without touching `unsafe`. This wasn’t obvious to me and my peers who’ve been using Rust for a while, so I thought adding a note to the docs would make it easier for others to discover.

If this should be worded differently or placed in another location, we can do that. I just want to make this more obvious.
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Sep 26, 2025
Rollup of 8 pull requests

Successful merges:

 - rust-lang/rust#116882 (rustdoc: hide `#[repr]` if it isn't part of the public ABI)
 - rust-lang/rust#135771 ([rustdoc] Add support for associated items in "jump to def" feature)
 - rust-lang/rust#141032 (avoid violating `slice::from_raw_parts` safety contract in `Vec::extract_if`)
 - rust-lang/rust#142401 (Add proper name mangling for pattern types)
 - rust-lang/rust#146293 (feat: non-panicking `Vec::try_remove`)
 - rust-lang/rust#146859 (BTreeMap: Don't leak allocators when initializing nodes)
 - rust-lang/rust#146924 (Add doc for `NonZero*` const creation)
 - rust-lang/rust#146933 (Make `render_example_with_highlighting` return an `impl fmt::Display`)

r? `@ghost`
`@rustbot` modify labels: rollup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants
0