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 sscanf;
let input = "5,something";
let mut number: u32 = 0;
let mut name: String = String new;
sscanf!.unwrap;
assert_eq!;
assert_eq!;
Escape brackets
# use sscanf;
let input: &str = "{Candy}";
let mut product: String = String new;
sscanf!.unwrap;
assert_eq!;
Examples
Enhanced approach with implicit capture
use sscanf;
let input: &str = "Candy: 2.75";
let mut product: String = String new;
let mut price: f32 = 0.0;
sscanf!.unwrap;
println!;
# assert_eq!;
# assert_eq!;
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 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!.unwrap;
assert_eq!;
assert_eq!;
assert_eq!;
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 sscanf;
let input = "5,something";
let mut number: u32 = 0;
let mut name: String = String new;
if let Err = sscanf!
Examples have been compiled and sscanf
's examples also run as tests.
If you have problems using the example code, please create an issue.