0365ab86d1
Add OsRandom::choose for callers that want a uniformly selected reference from any non-empty slice. This covers the common generic selection use case without adding collection algorithms such as shuffle. Keep OsRandom::pick as the byte-oriented helper by delegating it through choose. This preserves the existing public API while sharing the usize range sampler and avoiding the old u32 length limit. Document choose, pick, and string_from with working examples, and add a choices example that demonstrates generic slice selection alongside byte alphabets. Test Plan: - cargo test - cargo clippy - cargo clippy --benches - cargo clippy --tests - cargo +nightly fmt Refs: IDEAS.md ergonomics backlog
17 lines
420 B
Rust
17 lines
420 B
Rust
//! Run with: `cargo run --example choices`
|
|
|
|
use ez_urandom::{OsRandom, charset};
|
|
|
|
fn main() -> std::io::Result<()> {
|
|
let mut rng = OsRandom::try_new()?;
|
|
|
|
let environments = ["dev", "staging", "prod"];
|
|
let environment = rng.choose(&environments)?;
|
|
let suffix = rng.pick(charset::HEX_LOWER)?;
|
|
|
|
println!("environment: {environment}");
|
|
println!("hex suffix : {}", char::from(suffix));
|
|
|
|
Ok(())
|
|
}
|