3 ms·
For what it's worth, Rust isn't the only language that does this. OCaml also doesn't allow mixing ints and floats, and actually has entirely different operators
by aw1621107 4y ago
For what it's worth, Rust isn't the only language that does this. OCaml also doesn't allow mixing ints and floats, and actually has entirely different operators for int and float operations [0]. I think Haskell might do something similar [1]?
I don't know for sure why Rust chose this route, but I would guess it's a mix of type inference/type system considerations and a desire to avoid potential implicit loss of data.
[0]: https://stackoverflow.com/questions/64244351/why-does-ocaml-have-mandatory-distinct-float-and-int-literal-syntax https://stackoverflow.com/questions/64244351/why-does-ocaml-...
[1]: https://stackoverflow.com/questions/19019093/haskell-multiplying-int-and-float-within-a-function https://stackoverflow.com/questions/19019093/haskell-multipl...
- SAI_Peregrinus 4y agoRust just takes "explicit is better than implicit" further than most. No hidden type casts. Floating point operations aren't quite the same as integer operations. Float operations aren't commutative, for one. So it's nice to be suae you really intended to use a float operation on an integer by requiring an explicit cast, so you can get the order correct.
- agumonkey 4y agoAnd considering the influence of ml in rust roots it's not surprising.
- tome 4y agoIn Haskell you use the same operator for multiplying two Ints as you use for multiplying two Floats (you can't multiply an Int by a Float without converting one to the other).
- dllthomas 4y agoThere's also magic for numeric literals.