Crate scan_fmt [] [src]

This crate provides a simple sscanf()-like interface to extract data from strings and stdin.

To use this crate, do:

This example is not tested
#[macro_use] extern crate scan_fmt;

Example to read from a string:

  let (a,b,c) = scan_fmt!( "hello 12 345 bye", // input string
                           "hello {} {d} {}",  // format
                           u8, i32, String);   // type of a-c Options
  assert_eq!( a.unwrap(), 12 ) ;
  assert_eq!( b.unwrap(), 345 ) ;
  assert_eq!( c.unwrap(), "bye" ) ;

Special format_string tokens:

  {{ = 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]"
    {[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.
    {/[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

Example to read from stdin:

  let (a,b) = scanln_fmt!( "{}-{}",   // format
                           u16, u8);    // type of a&b Options
  match (a,b) {
    (Some(aa),Some(bb)) => println!("Got {} and {}",aa,bb),
    _ => println!("input error")


There are no compile-time checks to make sure the format strings matches the number of return arguments. Extra return values will be None.

Like sscanf(), whitespace (including \n) is largely ignored.

Conversion to output values is done using parse::().





(a,+) = scan_fmt!( input_string, format_string, types,+ )


(a,+) = scanln_fmt!( format_string, types,+ )