To generate a random boolean value in Rust, you can use the rand::Rng
trait from the standard library along with the gen_bool()
method. This method generates a randomly chosen boolean value with 50% probability of being either true
or false
. Here's an example:
main.rs158 chars8 lines
In this example, we first create a new rand::ThreadRng
instance using rand::thread_rng()
. We then call the gen_bool()
method on this instance, passing in a 0.5
probability of the boolean being either true
or false
. Finally, we print the value of the randomly generated boolean in the console.
Note that you'll need to add the rand
dependency to your Cargo.toml
file in order to use the rand::Rng
trait:
30 chars3 lines
gistlibby LogSnag