division_of_one/src/lib.rs

42 lines
1.0 KiB
Rust

use rand::seq::SliceRandom as _;
pub fn main() {
let passphrase = create_passphrase(" ", 8);
println!("{passphrase}");
println!("Press Enter");
let mut input = String::new();
std::io::stdin().read_line(&mut input).ok();
}
pub fn create_passphrase(separator: &str, len: usize) -> String {
let dice = Dice::default();
let words: Vec<_> = (0..len).map(|_| dice.pick_word()).collect();
words.join(separator)
}
pub struct Dice {
words: Vec<String>,
}
impl Default for Dice {
fn default() -> Self {
let wordlist = include_str!("eff_short_wordlist_1.txt");
let words: Vec<_> = wordlist.split('\n').map(|s| s.to_string()).collect();
assert_eq!(words.len(), 1253);
assert_eq!(words[0], "acid");
assert_eq!(words[600], "large");
assert_eq!(words[1252], "zoom");
Self { words }
}
}
impl Dice {
pub fn pick_word(&self) -> &str {
self.words
.choose(&mut rand::thread_rng())
.expect("`choose` should always return `Some`")
}
}