-
-
Notifications
You must be signed in to change notification settings - Fork 473
Description
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.