macro_rules! branch {
( $( $branch:expr ),* $(,)? ) => { ... };
}
Available on crate feature
std
only.Expand description
Branches into one of the given expressions.
This macro dose essentially the same as branch_using
instead of giving
it some RNG, this macro will simply use the rand::rng()
.
However, this then requires std
, unlike branch_using
.
This macro turns something like this:
branch!(
println!("First line."),
println!("Second line?"),
println!("Third line!"),
);
into something similar to this using the rand::rng()
:
match rand::rng().random_range(0..3) {
0 => println!("First line."),
1 => println!("Second line?"),
2 => println!("Third line!"),
_ => unreachable!(),
}
ยงExamples
You can use functions, macros and other arbitrary expressions:
use random_branch::branch;
fn do_something() {
println!("There is no such thing")
}
let thing = "fuliluf";
branch!(
println!("A {} is an animal!", thing),
{
let thing = "lufiful";
println!("Two {}s will never meet.", thing)
},
println!("Only a {} can see other {0}s.", thing),
do_something(),
);
You can also use it as an expression to yield some randomly chosen value:
use random_branch::branch;
let num = branch!(
10,
10 + 11,
2 * (10 + 11),
85,
);
println!("The best number is {}", num);