←back to thread

611 points LorenDB | 3 comments | | HN request time: 0s | source
Show context
writebetterc ◴[] No.43908008[source]
Yes, Rust is better. Implicit numeric conversion is terrible. However, don't use atoi if you're writing C++ :-). The STL has conversion functions that will throw, so separate problem.
replies(3): >>43908176 #>>43910253 #>>43910408 #
1. im3w1l ◴[] No.43910408[source]
Forcing people to explicitly casts everything all the time means that dangerous casts don't stand out as much. That's an L for rust imo.
replies(1): >>43910548 #
2. thesuperbigfrog ◴[] No.43910548[source]
The idiomatic Rust way to do conversions is using the From and TryFrom traits:

https://doc.rust-lang.org/stable/rust-by-example/conversion/...

https://doc.rust-lang.org/stable/rust-by-example/conversion/...

If the conversion will always succeed (for example an 8-bit unsigned integer to a 32-bit unsigned integer), the From trait would be used to allow the conversion to feel implicit.

If the conversion could fail (for example a 32-bit unsigned integer to an 8-bit unsigned integer), the TryFrom trait would be used so that an appropriate error could be returned in the Result.

These traits prevent errors when converting between types and clearly mark conversions that might fail since they return Result instead of the output type.

replies(1): >>43910665 #
3. im3w1l ◴[] No.43910665[source]
Thanks yeah I probably misremembered or misunderstood.