I'm not trying to write sophisticated code, I just want to understand what is (or is not) going on here. I checked other questions but they all had complicated situations and I think this situation is the simplest so far.
I have the following code:
let one_step: f32 = "4.0".parse().unwrap();
let extra_step: u32 = one_step as u32;
println!("{:?}", extra_step);
The way I see it, we have a &str
, we parse it to a f32
and unwrap it. Then we convert the f32
to a u32
.
Why can't I just do this? Isn't this, practically, the same thing?
let single_step: u32 = "4.0".parse().unwrap() as u32;
println!("{:?}", single_step);
If I try to run this code, I get this error:
error[E0619]: the type of this value must be known in this context
--> src/main.rs:6:27
|
6 | let single_step: u32 = "4.0".parse().unwrap() as u32;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
It looks like something requires us to break the operation into two chunks.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…