-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
consider for new epoch: rand::sample's API should not be infalible #46163
Comments
Another argument is that trying to sample when |
|
oh... I somehow didn't realize that. Closing this. |
I think this should be filed at https://github.com/rust-lang-nursery/rand, or rust-lang/rfcs#2106, or rust-lang/rfcs#2152. |
It's not even clear to me what |
I'm not sure where to put this, as it would be a breaking change and clearly it would need something like a new epoch to make happen, but I thought it would be worth mentioning.
Most rust operations are fallible if they don't make sense, either through a panic or by returning an
Option
orResult
. However,rand::sample
has the following API:This function will silently return less than amount if the size of
I
is less thanamount
. I would say this is very odd, and returningOption<Vec<T>>
would have been a better design choice. You can imagine the confusion if you didrand::sample(rng, v, 100)
but then only had 2 elements!If it were changed to returning
None
ifI.len() < amount
then the current behavior could still be accomplished with:With the current behavior, user has to put guards around their code (which is not required by the compiler) to prevent getting less than amount, which is unusual in rust.
The text was updated successfully, but these errors were encountered: