Skip to content

WeightedChoice is hard to use with borrow checker #142

@Binero

Description

@Binero

WeightedChoice is quite hard to use with Rust's borrow checker.

To create a WeightedChoice you need to give it an &'mut [Weighted]. This means that your [Weighted] is unavailable until WeightedChoice goes out of scope.

This makes it impossible to store a WeightedChoice in a structure, or return it from a function. Both scenarios would require you to also store or return your [Weighted], which is impossible because it's also borrowed by WeightedChoice.

As such, it's only possible to use a WeightedChoice inside the same scope that created the [Weighted], which means it is impossible to hide it away in an API.

It doesn't seem however, that WeightedChoice actually needs a mutable reference to the [Weighted]. It only needs this during its construction.

Metadata

Metadata

Assignees

No one assigned

    Labels

    F-new-intFunctionality: new, within Rand

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions