Macro sscanf::scanf[][src]

scanf!() { /* proc-macro */ }

A Macro to parse a String based on a format-String, similar to sscanf in C

Takes at least two Parameters:

  • An input string (String, str, … as long as it can be dereferenced into &str)
  • A format string literal (see below)

As well as any number of Types.

The format string has to be a str literal (with some form of " on either side), because it is parsed by the procedural macro at compile time and checks if all the types and placeholders are matched. This is not possible from inside a Variable or even a const &str somewhere else.

Placeholders within the format string are marked with {}. Any { or } that should not be treated as placeholders need to be escaped by writing {{ or }}. For any placeholder there has to be exactly one Type in the parameters after the format string.

There are currently no additional formatting options inside of the {}. This might be added later (no guarantees).

Examples

More examples can be seen in the crate root documentation.

use sscanf::scanf;

let input = "4-5 t: ftttttrvts";
let parsed = scanf!(input, "{}-{} {}: {}", usize, usize, char, String);
assert_eq!(parsed, Some((4, 5, 't', String::from("ftttttrvts"))));

let input = "<x=3, y=-6, z=6>";
let parsed = scanf!(input, "<x={}, y={}, z={}>", i32, i32, i32);
assert_eq!(parsed, Some((3, -6, 6)));

let input = "Goto N36E21";
let parsed = scanf!(input, "Goto {}{}{}{}", char, usize, char, usize);
assert_eq!(parsed, Some(('N', 36, 'E', 21)));