6 stable releases

1.2.1 Dec 12, 2021
1.2.0 Dec 9, 2021
0.2.3 Dec 1, 2021
0.2.1 Nov 30, 2021
0.0.0 Nov 27, 2021

#1562 in Parser implementations

Download history 1272/week @ 2023-12-03 1355/week @ 2023-12-10 978/week @ 2023-12-17 383/week @ 2023-12-24 852/week @ 2023-12-31 1305/week @ 2024-01-07 806/week @ 2024-01-14 793/week @ 2024-01-21 533/week @ 2024-01-28 385/week @ 2024-02-04 300/week @ 2024-02-11 321/week @ 2024-02-18 304/week @ 2024-02-25 181/week @ 2024-03-03 217/week @ 2024-03-10 193/week @ 2024-03-17

905 downloads per month
Used in 3 crates

Unlicense

18KB
388 lines

Scanf

If you know it from C, same functionality but with memory safety.

let mut number: u32 = 0;
let mut name: String = String::new();
if scanf!("{},{}", number, name).is_ok() {
    println!("Input is: {} and {}", number, name);
}
let input = "5,something";
let mut number: u32 = 0;
let mut name: String = String::new();
if let Err(error) = sscanf!(input, "{},{}", number, name) {
    panic!("Error {} using sscanf!", error);
}

Look more examples in the documentation.


lib.rs:

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.

Dependencies

~1MB
~17K SLoC