#match #random #choose #pick

no-std random-branch

Provides a macro to select a random branch like match

5 releases

0.2.0 Apr 25, 2025
0.1.1 Apr 8, 2021
0.1.0 Apr 7, 2021

#342 in Algorithms

Download history 227/week @ 2025-01-17 524/week @ 2025-01-24 1056/week @ 2025-01-31 427/week @ 2025-02-07 539/week @ 2025-02-14 581/week @ 2025-02-21 345/week @ 2025-02-28 469/week @ 2025-03-07 608/week @ 2025-03-14 657/week @ 2025-03-21 790/week @ 2025-03-28 691/week @ 2025-04-04 362/week @ 2025-04-11 352/week @ 2025-04-18 852/week @ 2025-04-25 374/week @ 2025-05-02

1,986 downloads per month

MIT/Apache

12KB
67 lines

Random Branch

Crates.io Documentation

Provides a macro to select a random branch.

This crate provides the branch and branch_using macro, which will execute randomly one of the given expressions.

It is maybe best visualized by the following example:

branch!(
    println!("First line."),
    println!("Second line?"),
    println!("Third line!"),
);

This will be turned into something similar to this:

match rand::thread_rng().random_range(0..3) {
    0 => println!("First line."),
    1 => println!("Second line?"),
    2 => println!("Third line!"),
    _ => unreachable!(),
}

For more details see branch and branch_using. The basic difference between them is, that branch uses rand::rng() whereas branch_using uses the the given rand::Rng.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~0.4–0.9MB
~15K SLoC