Crate rpick

source · []
Expand description


rpick helps pick items from a list of choices, using various algorithms.


use std::collections::BTreeMap;

use rand::SeedableRng;

/// You need to define an interface. rpick will use this interface to interact with you during
/// picks.
struct Interface {};

impl rpick::ui::Ui for Interface {
    fn call_display_table(&self) -> bool { false }

    fn display_table(&self, table: &rpick::ui::Table) {}

    fn info(&self, message: &str) { println!("{}", message); }

    fn prompt_choice(&self, choice: &str) -> bool {
        println!("{}", choice);

let ui = Interface{};
let mut engine = rpick::engine::Engine::new(&ui);
// For the sake of this example, let's override the PRNG with a seeded PRNG so the assertion
// works as expected at the end. You most likely do not want to do this in practice as it takes
// the randomness out of the system.
let choices = vec![String::from("this"), String::from("that"),
                   String::from("the other")];
let category = rpick::config::ConfigCategory::Even{choices: choices};
let mut config = BTreeMap::new();
config.insert("things".to_string(), category);

let choice = engine.pick(&mut config, "things".to_string()).expect("unexpected");

// 32-bit architectures have different PRNG results than 64-bit architectures, so we will
// only run this assertion on 64-bit systems.
#[cfg(target_pointer_width = "64")]
assert_eq!(choice, "that");



The Engine

The Ui Trait