Rustfmt: Formatting big numbers with thousand separator

Created on 2 Dec 2019  路  2Comments  路  Source: rust-lang/rustfmt

After using rustfmt v1.3.0 on the following one line and finding that it didn't change anything,

Utc::now() + Duration::seconds(1000000000)

I then realised later I should have written 1_000_000_000 for readability such as in

Utc::now() + Duration::seconds(1_000_000_000)

Is adding the underscore as a thousand separator something rustfmt would be interested in supporting?

feature-request good first issue help wanted wont-fix

All 2 comments

Sounds reasonable to me, though IMO it'd probably need to be user-controllable via a new config option (to support users that don't want the visual separator).

For anyone interested in implementing, I believe that would be done in rewrite_literal within src/expr.rs, presumably with a new match arm to handle ast::LitKind::Int

https://github.com/rust-lang/rustfmt/blob/7713d05c5bda4fb3bc3b01fd18b335b96d114d8a/src/expr.rs#L1218-L1231

I think that this feature is a bit too niche to add support in rustfmt and has too many scenarios where it doesn't work. A similar feature was once added to black and removed - see https://github.com/psf/black/issues/549 for the discussion.

The constant number normalization does not seem to work well in practice as the number tends to have its meaning. This is especially true in the context of the low-level system programming.

As such, I am going to close this as won't fix.

Was this page helpful?
0 / 5 - 0 ratings