Skip to content

Part of #164909: adding underscores to number literals for readability - #164927

Open
hexonide wants to merge 5 commits into
rust-lang:mainfrom
hexonide:underscore-literals
Open

hexonide wants to merge 5 commits into
rust-lang:mainfrom
hexonide:underscore-literals

Conversation

@hexonide

Copy link
Copy Markdown

Part of #164909
r? maxdexh


literal suffixes:

For consistency, I separated all suffixes, including ones attached to very short literals.

number literals:

For consistency, again, I changed 8 digit hex literals like this:

 assert_eq!(u32::wrapping_mul(0x9e3779b9, 0x7f4a7c15), 0xcffc982d);

to

assert_eq!(u32::wrapping_mul(0x9e37_79b9, 0x7f4a_7c15), 0xcffc_982d);

but I didn't change things like -1000_i32 to -1_000_i32 as I don't think that improves readability much.

should I continue like this? thanks.

@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 Oct 25, 2026
@rustbot

rustbot commented Oct 25, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome! The Rust Project has assigned @maxdexh (or someone else) to review your changes, you should hear from them (or someone else) within the next two weeks.

Please see the contribution instructions and our LLM policy for more information.

@rustbot

This comment has been minimized.

@hexonide
hexonide force-pushed the underscore-literals branch from fa1b75f to bca556d Compare October 25, 2026 19:08
@maxdexh

maxdexh commented Oct 25, 2026

Copy link
Copy Markdown
Member

Sorry, I don't have perms to approve PRs.
r? libs

@rustbot rustbot assigned thomcc and unassigned maxdexh Oct 25, 2026
Comment thread library/core/src/num/mod.rs Resolved
Comment thread library/core/src/num/int_macros.rs Resolved
Comment thread library/core/src/num/mod.rs Resolved
Comment thread library/core/src/num/mod.rs Resolved
#[doc = concat!("assert_eq!(3", stringify!($SelfT), ".overflowing_pow(5), (243, false));")]
#[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".overflowing_pow(0), (1, false));")]
/// assert_eq!(3u8.overflowing_pow(6), (217, true));
/// assert_eq!(3_u8.overflowing_pow(6), (217, true));

@bjorn3 bjorn3 Oct 26, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This doesn't feel like an improvement to me for tiny literals, but also not terrible either I guess.

View changes since the review

@sorairolake

sorairolake commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Following the behavior of clippy::unreadable_literal, I think it would be better to consider inserting underscores when number digits (number literals excluding literal prefixes [0b, 0o, 0x] and literal suffixes) have 6 or more digits. In other words, I think it is better not to insert underscores if number digits have fewer than 6 digits.

When inserting underscores, I think it is appropriate to use them as separators every 3 digits for octal and decimal literals, and every 4 digits for binary and hexadecimal literals.

@hexonide
hexonide force-pushed the underscore-literals branch from e7bd615 to ea297fc Compare October 27, 2026 03:56
@rust-log-analyzer

This comment has been minimized.

@thomcc

thomcc commented Nov 21, 2026

Copy link
Copy Markdown
Member

Following the behavior of clippy::unreadable_literal, I think it would be better to consider inserting underscores when number digits (number literals excluding literal prefixes [0b, 0o, 0x] and literal suffixes) have 6 or more digits. In other words, I think it is better not to insert underscores if number digits have fewer than 6 digits.

* https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=bd9020974ef9eaa4f0f201d06b2df422

* https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=c3cf5375278a9fcebca21a62fa9f5995

When inserting underscores, I think it is appropriate to use them as separators every 3 digits for octal and decimal literals, and every 4 digits for binary and hexadecimal literals.

I think @sorairolake's comment here is good. Do you mind making that change?

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 21, 2026
@rustbot

rustbot commented Nov 21, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@thomcc

thomcc commented Nov 21, 2026

Copy link
Copy Markdown
Member

Oh wait, it's already done.

@thomcc

thomcc commented Nov 21, 2026

Copy link
Copy Markdown
Member

Still, needs fixing. Do @rustbot ready once you've gotten it working again. Thanks!

@hexonide

hexonide commented Nov 22, 2026 •

Copy link
Copy Markdown
Author

Sorry for the empty commit, but I believe the test failure was spurious so I wanted to retry it. Thanks for reviewing!
@rustbot ready

@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Nov 22, 2026
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 22, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants