Working with numbers
Numbers look settled until they are not: an integer that wraps in release and panics in debug, a float comparison that is never true, a thousands separator the standard library can already print.
Five of these articles are conversions. The rest are the traps.
Articles
Section titled “Articles”- How to convert a String to a numberparse, and the error it returns
- How to convert a number to a Stringto_string, format!, and the no-allocation case
- How to parse an integer from user inputreading, trimming, and failing without panicking
- How to stop an integer from overflowingdebug panics, release wraps, and what to write instead
- How to choose between i32 and usizewhat each one means, and where it bites
- What is the difference between f32 and f64?precision, memory, and the default
- What is the difference between as and From for casting?truncation versus an error you must handle
- How to generate a random number in a rangerand, and the inclusive range trap
- How to round a float to a number of decimalswhy round() returns a float, and when to format instead
- How to compare floats without surprisesepsilon, and the NaN that breaks every comparison
- How to format a number with thousands separatorsthe format spec the standard library already has
- How to divide integers and get the remaindertruncation, %, and the negative number that surprises you