The trait `From<u32>` is not implemented for `f32`

Why is the trait std::convert::From<u32> not implemented for f32?
I have a struct: Struct<T> and I need to convert it into Struct<R>, if T can be converted into R. I use the R: From<T> bound for conversion. How can I do this properly?
Moreover, why can't I convert u32 into f32?

The From trait is only implemented for lossless conversions. Not every u32 can be represented as an f32. For example:

println!("{}", 4000000001u32 as f32); // prints 4000000000

You can use the NumCast trait from the num-traits package if you want to allow approximate conversions.

3 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.