#random #choose #match

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

#472 in Algorithms

Download history 453/week @ 2025-05-17 378/week @ 2025-05-24 276/week @ 2025-05-31 848/week @ 2025-06-07 353/week @ 2025-06-14 444/week @ 2025-06-21 226/week @ 2025-06-28 357/week @ 2025-07-05 382/week @ 2025-07-12 668/week @ 2025-07-19 401/week @ 2025-07-26 543/week @ 2025-08-02 775/week @ 2025-08-09 1191/week @ 2025-08-16 348/week @ 2025-08-23 830/week @ 2025-08-30

3,268 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.8MB
~14K SLoC