scan_fmt provides a simple scanf()-like input for Rust. The goal is to make it easier to read data from a string or stdin.
Currently the format string supports the following special sequences:
{{ = escape for '{' }} = escape for '}' {} = return any value (until next whitespace) {d} = return base-10 decimal {x} = return hex (0xab or ab) {f} = return float {*d} = "*" as the first character means "match but don't return" {[...]} = return pattern. ^ inverts if it is the first character - is for ranges. For a literal - put it at the start or end. To add a literal ] do "[]abc]" Examples: {[0-9ab]} = match 0-9 or a or b {[^,.]} = match anything but , or . {/.../} = return regex inside of `//`. If there is a single capture group inside of the slashes then that group will make up the pattern. Examples: {/[0-9ab]/} = same as {[0-9]ab}, above {/a+/} = matches at least one `a`, greedily {/jj(a*)jj/} = matches any number of `a`s, but only if they're surrounded by two `j`s
```rust #[macrouse] extern crate scanfmt; fn main() { let (a,b,c) = scanfmt!( "hello 12 345 bye", // input string "hello {} {} {}", // format u8, i32, String); // type of a-c Options asserteq!( a.unwrap(), 12 ) ; asserteq!( b.unwrap(), 345 ) ; asserteq!( c.unwrap(), "bye" ) ;
println!("Enter something like: 123-22"); let (c,d) = scanlnfmt!( "{d}-{d}", // format u16, u8); // type of a&b Options match (c,d) { (Some(cc),Some(dd)) => println!("Got {} and {}",cc,dd), _ => println!("input error") } // Note - currently scanlnfmt! just calls unwrap() on read_line() } ```
There is no compile-time warning if the number of {}'s in the format string doesn't match the number of return values. You'll just get None for extra return values. See src/lib.rs for more details.