I need to parse a file with on each line
<string><space><int><space><float>
e.g.
abce 2 2.5
In C I would do:
scanf("%s%d%f", &s, &i, &f);
How can I do this easily and idiomatically in Rust?
The standard library doesn't provide this functionality. You could write your own with a macro.
macro_rules! scan { ( $string:expr, $sep:expr, $( $x:ty ),+ ) => {{ let mut iter = $string.split($sep); ($(iter.next().and_then(|word| word.parse::<$x>().ok()),)*) }} } fn main() { let output = scan!("2 false fox", char::is_whitespace, u8, bool, String); println!("{:?}", output); // (Some(2), Some(false), Some("fox")) }
The second input argument to the macro can be a &str, char, or the appropriate closure / function. The specified types must implement the FromStr trait.
Note that I put this together quickly so it hasn't been tested thoroughly.
You can use the text_io
crate for scanf-like input that mimicks the print!
macro in syntax
#[macro_use] extern crate text_io; fn main() { // note that the whitespace between the {} is relevant // placing any characters there will ignore them but require // the input to have them let (s, i, j): (String, i32, f32); scan!("{} {} {}\n", s, i, j); }
You can also split it into 3 commands each:
#[macro_use] extern crate text_io; fn main() { let a: String = read!("{} "); let b: i32 = read!("{} "); let c: f32 = read!("{}\n"); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With