I'm trying to write a function that will parse float from given string. It should return error in case of wrong or negative value.
fn read_value(strvalue: &str) -> Result<f32, Error> {
match FromStr::from_str(strvalue) {
None => Err(Error::InvalidValue),
Some(value) => if value >= 0.0 {Ok(value)} else {Err(Error::InvalidValue)}
}
}
This code gives:
src/main.rs:50:27: 50:32 error: the type of this value must be known in this context
src/main.rs:50 Some(value) => if value >= 0.0 {Ok(value)} else {Err(Error::InvalidValue)}
The first point. This error seems strange to me because, if I understand correctly, type of value
can be inferred automatically. From the result type the type of value
must be f32
.
The second question. How can I fix this error? Or more general - how to annotate types of expressions in Rust?
E.g. in Haskell I can write something like:
if (value :: f32) > 0.0 ...
Or put type annotation in pattern match::
Some(value :: f32) => ...