From da9d5f96d84e297b7689bdaed4d4048dedc80119 Mon Sep 17 00:00:00 2001 From: ddidderr Date: Tue, 28 Apr 2026 19:17:06 +0200 Subject: [PATCH] docs(examples): add runnable demo example Adds examples/demo.rs so users can quickly see the crate in action via `cargo run --example demo` without writing any glue code first. The example exercises the three main surface areas of the crate: - the typed primitive readers (get_u8, get_u32, get_u64, get_i32) to show that every integer width is covered and that signed values come out signed; - gen_range_u32 in a dice-roll idiom (+ 1 to shift 0..6 into 1..6), which doubles as a hint that the helper returns a half-open range; - string_from with three of the prebuilt charsets (ALPHANUMERIC for a generic token, HEX_LOWER for a 128-bit-style hex string, DIGITS for a numeric PIN), demonstrating the typical "generate me a random identifier" use case the crate is designed for. No library code changes; this is purely an onboarding aid. Picked an example over expanding the crate-level rustdoc because a runnable binary is easier to copy-paste-modify than a doctest, and Cargo's examples/ convention is the idiomatic place for this. Test Plan: - `cargo run --example demo` prints one line per demonstrated API and exits 0. - `cargo clippy --all-targets` is clean. --- examples/demo.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 examples/demo.rs diff --git a/examples/demo.rs b/examples/demo.rs new file mode 100644 index 0000000..69c0cd9 --- /dev/null +++ b/examples/demo.rs @@ -0,0 +1,25 @@ +//! Run with: `cargo run --example demo` + +use ez_urandom::{OsRandom, charset}; + +fn main() -> std::io::Result<()> { + let mut rng = OsRandom::try_new()?; + + println!("u8 : {}", rng.get_u8()?); + println!("u32 : {}", rng.get_u32()?); + println!("u64 : {}", rng.get_u64()?); + println!("i32 : {}", rng.get_i32()?); + + println!("dice 1-6 : {}", rng.gen_range_u32(6)? + 1); + + let token = rng.string_from(charset::ALPHANUMERIC, 24)?; + println!("token : {token}"); + + let hex = rng.string_from(charset::HEX_LOWER, 32)?; + println!("hex : {hex}"); + + let pin = rng.string_from(charset::DIGITS, 6)?; + println!("pin : {pin}"); + + Ok(()) +}