Skip to content

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

Closed
@PatchMixolydic

Description

@PatchMixolydic

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions