To generate a random number in Rust, the standard library provides the rand
crate which handles random number generation. Here's an example code snippet that generates a random number between 0 and 100:
main.rs165 chars8 lines
In the above code, rand::thread_rng()
initializes a random number generator from the current thread, and then rng.gen_range(0, 101)
generates a random number between 0 (inclusive) and 101 (exclusive). The random number is then printed to the console using a println!
macro.
Note that you need to include the rand
dependency in your Cargo.toml
:
30 chars3 lines
This should get you started with generating random numbers in Rust.
gistlibby LogSnag