Why is there no try_into for float to integer conversions

I'm sure this has been asked before, but I couldn't find it while googling. Hopefully someone can point me in the right direction.

Why is there no try_into to convert floats to integers or fail if the conversion would be lossy? It seems like such an implementation would be well defined and useful.

Feels like it would be ambiguous between rounding and only failing if it would overflow, and the behavior you describe.

I would prefer something better than having to use as though.

2 Likes

I think it is because there are multiple reasonable implementations for failable float to int conversion. See discussion here: Implement TryFrom for float to integer types by udoprog · Pull Request #47857 · rust-lang/rust · GitHub

Additionall there is Conversions: `FromLossy` and `TryFromLossy` traits by dhardy · Pull Request #2484 · rust-lang/rfcs · GitHub

3 Likes