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

Improve documentation for rand::random #15746

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions src/libstd/rand/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,19 +226,24 @@ impl Rng for TaskRng {
}
}

/// Generate a random value using the task-local random number
/// generator.
/// Generates a random value using the task-local random number generator.
///
/// # Example
/// `random()` can generate various types of random things, and so may require
/// type hinting to generate the specific type you want.
///
/// # Examples
///
/// ```rust
/// use std::rand::random;
/// use std::rand;
///
/// let x = rand::random();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason you removed the if random() part of the example as well?

Also, is it worth having a comment here indicating the inference is running "backwards"?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for re-addition of if random()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, without explanation, I had no idea why it was even there. Now that you've mentioned it, it's to demonstrate that it can generate a boolean? I'd be about putting that back with a comment explaining that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was there to demonstrate a variety of ways in which the type of the return value can be inferred.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this makes sense looking back. Without context, I had no idea.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, other people have actually had confusion too for the similar example in the main module doc string: http://stackoverflow.com/q/24153311/1256624

/// println!("{}", 2u * x);
///
/// let y = rand::random::<f64>();
/// println!("{}", y);
///
/// if random() {
/// let x = random();
/// println!("{}", 2u * x);
/// } else {
/// println!("{}", random::<f64>());
/// if rand::random() { // generates a boolean
/// println!("Better lucky than good!");
/// }
/// ```
#[inline]
Expand Down