Crate scanf

Source
Expand description

§scanf! & sscanf!

Similar to C’s but with memory safety.

§Examples

use scanf::scanf;

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

let input: &str = "Candy: 2.75";
let mut product: String = String::new();
let mut price: f32 = 0.0;
println!("Insert product and price (product: price):");
sscanf!(input, "{}: {}", product, price);
println!("Price of {} is {:.2}", product, price);

It’s possible to indicate the type in the format string:

let mut product: String = String::new();
let mut price: f32 = 0.0;
println!("Insert product and price (product: price):");
scanf!("{string}: {f32}", product, price);

Also escape brackets:

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

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

Macros§