rust-random / rand

A Rust library for random number generation.
https://crates.io/crates/rand
Other
1.6k stars 423 forks source link

CHANGE: Rename `Rng::gen` to avoid conflicting with a keyword in Rust 2024 #1435

Closed PatchMixolydic closed 2 months ago

PatchMixolydic commented 2 months ago

Forgive me if this has been posted before, but I couldn't find an issue tracking this.

Summary

RFC 3513, which reserves gen as a keyword for generators in Rust 2024, has been accepted (tracking issue). This conflicts with the naming of Rng::gen. This issue proposes renaming or adding an alias for Rng::gen.

Details

The internal implementation is the easy part: Rng::gen should either be removed or deprecated, and a new method with the same functionality should be added. The difficult part would be deciding on what this new method should be called. This issue is meant to provide an avenue for bikeshedding the new name.

One possibility could be Rng::generate, though this seems slightly unwieldy.

Motivation

In Rust 2024, gen is slated to become a keyword. This means that anyone who calls Rng::gen will have to use a raw identifier:

let x = rng.r#gen::<u32>();

// Alternatively
let y: u32 = rng.r#gen();

This is suboptimal for several reasons: it requires the use of a somewhat obscure feature, it is much less readable at a glance, it's more difficult to type, it runs the risk of turning into sigil soup (especially when using a turbofish), etc. Picking a different name would improve the UX of this common method in Rust 2024 and beyond.

Alternatives

Do nothing

Rng::gen will stay as it is, and anyone who uses Rust 2024 and above will have to call it using a raw identifier. This is suboptimal for the reasons detailed in the Motivation section, but avoids the resulting ecosystem churn from renaming a common method.

Convince the Rust language team to adopt a different keyword

As can be seen on the tracking issue, whether gen is the right keyword for generators is an unresolved question. Since rand uses gen prominently, it might be worthwhile for rust-random to weigh in if it hasn't already.

Remove Rng::gen

As far as I can tell, Rng::gen is a convenience alias for Rng::sample(Standard). This implies that Rng::gen could be removed entirely. However, this would cause just as much churn as renaming Rng::gen, and maintainers would probably want to understand what Standard and Rng::sample are before switching to them. This would also create a pain point for newcomers, as they'd no longer have an obvious starting point for generating an arbitrary random value for a given type.

newpavlov commented 2 months ago

Renaming it to something like gen_value looks like a good solution.

dhardy commented 2 months ago

Thanks for raising this with us @PatchMixolydic. This gives us strong motivation for actually getting a new release out this year!

In other languages, it can be common practice to do something like let sample = rng.gen() * 2.0 + 1.0, but we already have gen_range. So, is this method actually used much? We could rename to standard_sample or deprecate (and the same for gen_iter, leaving only gen_range using gen_*).

dhardy commented 2 months ago

There are a lot of offending uses of gen in this repo:

Probably then quite a few users of this library will be impacted.

vks commented 2 months ago

We should probably keep Rng::gen to make the migration to the new edition easier, but we might want to deprecate it. (I think cargo can automatically add r# when migrating to a new edition.)

Rng::random could be a good alternative name, because Rng::gen is the analogue to rand::random.

TheIronBorn commented 2 months ago

Reusing the sample keyword (like let x: u8 = rng.sample();) might be more intuitive, at least for Distribution. Though the overlapping traits/etc might also be more confusing

dhardy commented 2 months ago

Rng::random could be a good alternative name

Best choice yet

Reusing the sample keyword

We already have Rng::sample though: rng.sample(Standard)