#Help with parsing a string split into a value

1 messages · Page 1 of 1 (latest)

white cove
#

Every time I split a string and then try to convert that split into a float, I get an error from the parse:

code:

        let mut num_first = split_input.clone().rev().last().expect("First value was not a number.").parse::<f64>().unwrap();```

error:
```thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: ParseFloatError { kind: Invalid }', src\main.rs:14:109
stack backtrace:
   0: std::panicking::begin_panic_handler
             at /rustc/8460ca823e8367a30dda430efda790588b8c84d3/library\std\src\panicking.rs:575
   1: core::panicking::panic_fmt
             at /rustc/8460ca823e8367a30dda430efda790588b8c84d3/library\core\src\panicking.rs:64
   2: core::result::unwrap_failed
             at /rustc/8460ca823e8367a30dda430efda790588b8c84d3/library\core\src\result.rs:1790
   3: enum2$<core::result::Result<f64,core::num::dec2flt::ParseFloatError> >::unwrap<f64,core::num::dec2flt::ParseFloatError>
             at /rustc/8460ca823e8367a30dda430efda790588b8c84d3\library\core\src\result.rs:1112
   4: ConsoleCalculatorRUST::main
             at .\src\main.rs:14
   5: core::ops::function::FnOnce::call_once<void (*)(),tuple$<> >
             at /rustc/8460ca823e8367a30dda430efda790588b8c84d3\library\core\src\ops\function.rs:250
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.```
barren needle
#

You should check what the strings actually are to find out what the problem is

#

you could add dbg!(split_input.clone().collect::<Vec<&str>>());

#

I bet there is some kind of whitespace or an empty string

#

also, tip: .rev().last() can be replaced with .next()