Closed PatchMixolydic closed 2 months ago
Renaming it to something like gen_value
looks like a good solution.
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_*
).
There are a lot of offending uses of gen
in this repo:
Bernoulli
compares a standard sample against its probabilityStandard
impls for signed and non-zero ints over impls for unsigned intsOption
and Wrapping
impls of Standard
Fill
implsseq::index::sample_efraimidis_spirakis
uses a standard f64 samplerand::random
Probably then quite a few users of this library will be impacted.
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
.
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
Rng::random
could be a good alternative name
Best choice yet
Reusing the
sample
keyword
We already have Rng::sample
though: rng.sample(Standard)
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 ofRng::gen
. This issue proposes renaming or adding an alias forRng::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 callsRng::gen
will have to use a raw identifier: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. Sincerand
usesgen
prominently, it might be worthwhile forrust-random
to weigh in if it hasn't already.Remove
Rng::gen
As far as I can tell,
Rng::gen
is a convenience alias forRng::sample(Standard)
. This implies thatRng::gen
could be removed entirely. However, this would cause just as much churn as renamingRng::gen
, and maintainers would probably want to understand whatStandard
andRng::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.