I have a Vec<f32> with zero values that I'd like to convert into NaN. Is there a way to do this by modifying the Vec in-place with a conditional statement?
This is what I tried so far:
let mut vector: Vec<f32> = vec![2.0, 0.0, 1.0, 3.0, -1.0, 5.0];
vector
.iter_mut()
.for_each(|v| if *v == 0.0 { f32::NAN } else { *v });
println!("{vector:?}");
But the compiler is giving me this error:
error[E0308]: mismatched types
--> src/io/mod.rs:8:38
|
8 | .for_each(|v| if *v == 0.0 { f32::NAN } else { *v });
| ---------------^^^^^^^^--------------
| | |
| | expected `()`, found `f32`
| expected this to be `()`
error[E0308]: mismatched types
--> src/io/mod.rs:8:56
|
8 | .for_each(|v| if *v == 0.0 { f32::NAN } else { *v });
| ---------------------------------^^--
| | |
| | expected `()`, found `f32`
| expected this to be `()`
For more information about this error, try `rustc --explain E0308`.
Can someone point out what the proper syntax should be to get a Vec with 0 replaced with NaN?
You didn’t do any writing:
More typically: