Skip to content
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

Closed
vitiral opened this issue Nov 21, 2017 · 6 comments
Closed

consider for new epoch: rand::sample's API should not be infalible #46163

vitiral opened this issue Nov 21, 2017 · 6 comments

Comments

@vitiral
Copy link
Contributor

vitiral commented Nov 21, 2017

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 or Result. However, rand::sample has the following API:

pub fn sample<T, I, R>(rng: &mut R, iterable: I, amount: usize) -> Vec<T> where
    I: IntoIterator<Item = T>,
    R: Rng, 

Randomly sample up to amount elements from a finite iterator.

This function will silently return less than amount if the size of I is less than amount. I would say this is very odd, and returning Option<Vec<T>> would have been a better design choice. You can imagine the confusion if you did rand::sample(rng, v, 100) but then only had 2 elements!

If it were changed to returning None if I.len() < amount then the current behavior could still be accomplished with:

rand::sample(&mut rng, vec.iter(), usize::min(amount, vec.len())).unwrap()

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.

@vitiral
Copy link
Contributor Author

vitiral commented Nov 21, 2017

Another argument is that trying to sample when amount > len() will just return an "inadequately shuffled" version of the iterator. If someone wanted that behavior it would be more correct to just collect() the iterator and call shuffle() on it.

@hanna-kruppe
Copy link
Contributor

rand is a crates.io library, not part of the standard library distributed with the compiler, so it can release a new major version without breaking anything (well, except people with rand = "*" in their Cargo.toml, I guess).

@vitiral
Copy link
Contributor Author

vitiral commented Nov 21, 2017

oh... I somehow didn't realize that. Closing this.

@vitiral vitiral closed this as completed Nov 21, 2017
@kennytm
Copy link
Member

kennytm commented Nov 21, 2017

@vitiral
Copy link
Contributor Author

vitiral commented Nov 21, 2017

rust-random/rand#194

@canndrew
Copy link
Contributor

It's not even clear to me what sample does, based on the code example in its documentation :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants