Skip to main content

weighted_random

Macro weighted_random 

Source
macro_rules! weighted_random {
    (@parse {} -> ($($weights:expr,)*) ($($bodies:expr,)*)) => { ... };
    (@parse {, $($rest:tt)*} -> ($($weights:expr,)*) ($($bodies:expr,)*)) => { ... };
    (@parse {$weight:expr => $body:block $($rest:tt)*} -> ($($weights:expr,)*) ($($bodies:expr,)*)) => { ... };
    (@parse {$body:block $($rest:tt)*} -> ($($weights:expr,)*) ($($bodies:expr,)*)) => { ... };
    (@parse {$weight:expr => $body:expr, $($rest:tt)*} -> ($($weights:expr,)*) ($($bodies:expr,)*)) => { ... };
    (@parse {$weight:expr => $body:expr} -> ($($weights:expr,)*) ($($bodies:expr,)*)) => { ... };
    (@parse {$body:expr, $($rest:tt)*} -> ($($weights:expr,)*) ($($bodies:expr,)*)) => { ... };
    (@parse {$body:expr} -> ($($weights:expr,)*) ($($bodies:expr,)*)) => { ... };
    (@parse $($_rest:tt)*) => { ... };
    ($($input:tt)*) => { ... };
}
Expand description

Picks one of several branches at random, weighted by the per-branch weights, and evaluates the chosen branch as an expression (its value is the value of the macro).

Each branch is either weight => body or just body (implied weight 1u32). Weights must be u32 expressions; bodies must all evaluate to the same type. Branches are separated by commas; trailing block bodies may omit the comma.