How to extract multiple floats from a string of floats in Rust

1 Answer

0 votes
fn main() {
    let input_str = "2.809 -36.91 21.487 -493.808 5034.7001";
    let mut floats: Vec<f32> = Vec::new();

    for token in input_str.split_whitespace() {
        if let Ok(f) = token.parse::<f32>() {
            println!("{}", f);
            floats.push(f);
        }
    }
    
    println!("{:?}", floats);
}




/*
run:

2.809
-36.91
21.487
-493.808
5034.7
[2.809, -36.91, 21.487, -493.808, 5034.7]

*/

 



answered Jul 28, 2024 by avibootz
...