To generate a list of 5 random numbers in Rust, you will need the rand
crate. First, add rand
to your dependencies in Cargo.toml
:
30 chars3 lines
Then, you can use the gen_range
method of the Rng
trait to generate each random number, and add it to a vector to store the list of numbers:
main.rs278 chars14 lines
This code creates a new thread_rng
and initializes an empty vector to hold the random numbers. Then, it loops 5 times, generating a random number between 1 and 100 using gen_range
and adding it to the vector. Finally, it prints out the entire vector of random numbers.
gistlibby LogSnag