From 96be5aba6caed16dd490652b49c6f4307b32e226 Mon Sep 17 00:00:00 2001 From: John Date: Thu, 19 Sep 2024 14:08:50 -0500 Subject: [PATCH] repline: Add a code sample demonstrating the use of prebaked::read_and --- repline/examples/repl_float.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 repline/examples/repl_float.rs diff --git a/repline/examples/repl_float.rs b/repline/examples/repl_float.rs new file mode 100644 index 0000000..c72ea89 --- /dev/null +++ b/repline/examples/repl_float.rs @@ -0,0 +1,17 @@ +//! Demonstrates the use of [read_and()]: +//! +//! The provided closure: +//! 1. Takes a line of input (a [String]) +//! 2. Performs some calculation (using [FromStr]) +//! 3. Returns a [Result] containing a [Response] or an [Err] + +use repline::{prebaked::read_and, Response}; +use std::{error::Error, str::FromStr}; + +fn main() -> Result<(), Box> { + read_and("\x1b[33m", " >", " ?>", |line| { + println!("-> {:?}", f64::from_str(line.trim())?); + Ok(Response::Accept) + })?; + Ok(()) +}