Skip to main content

Crate scanf

Crate scanf 

Source
Expand description

C-style scanf/sscanf procedural macros for text parsing.

  • scanf!: Read and parse from stdin
  • sscanf!: Parse from string

§Architecture

Compile-time: tokenization → codegen → expansion Runtime: Generated code uses .find() and .parse()

Modules: constants, types, validation, parsing, tokenization, codegen

§Hygiene

Generated code uses isolated scopes - no variable pollution.

§Limitations

  • Consecutive placeholders {}{} not allowed (ambiguous)
  • {} arguments are all or nothing: one &mut per {}, or none and get the values back
  • Greedy parsing (no backtracking)
  • Types must implement FromStr
  • scanf! trims trailing newlines
  • Surrounding whitespace is kept unless the trim-whitespace feature is enabled (see README)

§Security

DoS limits: 10K bytes format, 256 tokens, 128 char identifiers Memory: #![forbid(unsafe_code)], Box<str>, bounds-checked Validation: Rejects empty formats, keywords, invalid identifiers

§Scanf

If you know it from C, same functionality but with memory safety, plus new enhanced features!

§Usage

Like Rust’s format! macro, scanf supports automatic variable capture:

use scanf::scanf;

let mut number: u32 = 0;
let mut name: String = String::new();
if scanf!("{number},{name}").is_ok() {
    println!("Input is: {} and {}", number, name);
}
use scanf::sscanf;

let input = "5,something";
let mut number: u32 = 0;
let mut name: String = String::new();

sscanf!(input, "{number},{name}").unwrap();
assert_eq!(number, 5);
assert_eq!(name, "something");

§Returning the values

If you don’t pass any arguments, {} placeholders are returned instead of written into variables. You don’t have to create the variables first, and the types come from your annotations:

use scanf::sscanf;

let age: u32 = sscanf!("age=30", "age={}").unwrap();
assert_eq!(age, 30);

// Several values come back as a tuple
let (name, score): (String, f64) = sscanf!("Ana: 9.5", "{}: {}").unwrap();
assert_eq!(name, "Ana");
assert_eq!(score, 9.5);

It’s all or nothing: either every {} gets a &mut argument, or none does. Named placeholders like {x} still write into their variable, and they work alongside returned values. scanf! works the same way.

§Escape brackets

let input: &str = "{Candy}";
let mut product: String = String::new();
sscanf!(input, "{{{product}}}").unwrap();
assert_eq!(product, "Candy");

§Examples

§Enhanced approach with implicit capture

use scanf::sscanf;

let input: &str = "Candy: 2.75";
let mut product: String = String::new();
let mut price: f32 = 0.0;

sscanf!(input, "{product}: {price}").unwrap();
println!("Price of {} is {:.2}", product, price);

§Traditional approach

use scanf::scanf;

let mut product: String = String::new();
let mut price: f32 = 0.0;
println!("Insert product and price (product: price):");
if scanf!("{}: {}", &mut product, &mut price).is_ok() {
    println!("Price of {} is {:.2}", product, price);
}

§Mixed syntax - named and anonymous placeholders

use scanf::sscanf;

let input: &str = "Alice: 25 years";
let mut name: String = String::new();
let mut age: i32 = 0;
let mut unit: String = String::new();

sscanf!(input, "{name}: {} {unit}", &mut age).unwrap();
assert_eq!(name, "Alice");
assert_eq!(age, 25);
assert_eq!(unit, "years");

§Traditional Syntax

use scanf::scanf;

let mut number: u32 = 0;
let mut name: String = String::new();
if scanf!("{},{}", &mut number, &mut name).is_ok() {
    println!("Input is: {} and {}", number, name);
}
use scanf::sscanf;

let input = "5,something";
let mut number: u32 = 0;
let mut name: String = String::new();
if let Err(error) = sscanf!(input, "{},{}", &mut number, &mut name) {
    panic!("Error {} using sscanf!", error);
}

§Features

§trim-whitespace (disabled by default)

By default every captured value is parsed exactly as it appears in the input, so " 42 " is not a valid integer. Enabling this feature makes a capture that fails to parse be retried once with its surrounding whitespace trimmed:

scanf = { version = "2", features = ["trim-whitespace"] }
sscanf!(" 42 ", "{number}")      // number: i32      -> 42
sscanf!("  hi  ,7", "{s},{n}")   // s: String        -> "  hi  " (kept as is)
sscanf!("1 2", "{number}")       // number: i32      -> error (only the edges are trimmed)

How it works and what to keep in mind:

  • The macro cannot see the type of the variable, so the rule is “retry on failure”, not “trim numbers”. A String never fails to parse, so it always keeps its whitespace.
  • Only leading and trailing whitespace is removed. Whitespace between two values is never interpreted: "1 2" into a single number is still an error.
  • Limitation: the retry applies to every type implementing FromStr. A custom type whose FromStr deliberately rejects surrounding whitespace, but accepts the trimmed text, will accept it with this feature enabled.
  • If both attempts fail, the error of the first (untrimmed) attempt is reported.
  • Trailing newlines read by scanf! are always removed, with or without this feature.

Examples have been compiled and sscanf’s examples also run as tests. If you have problems using the example code, please create an issue.

Macros§

scanf
Read from stdin and parse with a format string, similar to C’s scanf.
sscanf
Parse a string with a format string, similar to C’s sscanf.