I am following along the learn rust book from the rust lang website and random number generation is not working.
Specifically, when trying to create a random range like so:
use rand::Rng;
fn main() {
let s: u32 = rand::thread_rng().gen_range(1, 101);
println!("{}", s);
}
I get the error:
Checking learn-rust v0.1.0 (.../learn-rust)
error[E0061]: this function takes 1 argument but 2 arguments were supplied
--> src/main.rs:8:37
|
8 | let s: u32 = rand::thread_rng().gen_range(1, 101);
| ^^^^^^^^^ - --- supplied 2 arguments
| |
| expected 1 argument
error: aborting due to previous error
For more information about this error, try `rustc --explain E0061`.
error: could not compile `learn-rust`
To learn more, run the command again with --verbose.
In the rust playground I get the same error?
The version of rand on both local and playground is 0.8.0
.
The easiest way to resolve an error like this is to search for gen_range
in the docs for the crate (rand). You'll find that it's a method of the Rng
trait, and it takes a single argument: the range. So, supply it with a range:
rand::thread_rng().gen_range(1..101)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With