int and float are the default numeric types. Fixed-width spellings include
i8, i16, i32, i64, i128, isize, u8, u16, u32, u64,
u128, usize, f32, and f64; byte is the byte-sized unsigned type.
count := 42
ratio := 0.75
mask: u32 = 0xFFFFu32
precise: f32 = 1.25f32int is the same 64-bit signed type as i64; float is the same 64-bit
IEEE-754 type as f64. Unsuffixed integer and floating literals therefore
default to those two types.
Integers
Signed and unsigned families provide fixed-width storage for binary and host
boundaries. Common helpers include min, max, clamp, string conversion,
and explicit width conversions; signed types also provide abs.
Default integer arithmetic wraps to the destination width instead of panicking. When overflow is part of the domain contract, choose an explicit family:
| Family | Overflow behavior |
|---|---|
wrapping_add, wrapping_sub, wrapping_mul |
Wrap modulo the type width |
saturating_add, saturating_sub, saturating_mul |
Clamp at the numeric limit |
checked_add, checked_sub, checked_mul |
Return None on overflow |
checked_div |
Return None for invalid division |
bounded := value.clamp(0, 100)
next := value.checked_add(delta)
wrapped := counter.wrapping_add(1)
small := value.try_to_u8()Use the try_to_* methods when a narrowing conversion must reject values
outside the target range. Direct narrowing conversions are wrapping
conversions; signed-to-unsigned methods may reinterpret the low-width bit
pattern. Reserve them for code with an established range or representation
invariant.
Integer / truncates toward zero and % returns its corresponding remainder.
Use checked_div whenever zero or the signed minimum divided by -1 can reach
the operation.
Floating point
Floating types provide:
ceil,floor,round,round_to, andtruncate;sqrt,cbrt,pow, logarithms, and exponentiation;- sine, cosine, tangent, and inverse trigonometry;
min,max,clamp, and sign queries;- IEEE classification with
is_nan,is_infinite,is_finite, andis_normal.
root := value.sqrt()
rounded := value.round_to(2)
finite := value.is_finite()
parsed := float.from_string(text)Floating division by zero produces IEEE infinity or NAN. Domain errors such
as the square root of a negative value also produce NAN; they are not typed
errors. Test classification when those values must be rejected.
Ordinary comparisons follow IEEE behavior, where NAN is unordered. Use
total_compare for sorting or deterministic keys that must place every bit
pattern, including NAN.
Conversions
Integer-to-float conversion can lose precision for large magnitudes.
Float-to-integer methods truncate toward zero. Narrowing from float to f32
can lose both precision and range.
Use usize and isize for layout, collection capacity, and pointer-adjacent
operations. Do not expose pointer-sized values in a stable file or network
format; use an explicit fixed width instead.
Parsing and formatting
match int.from_string(text) {
Some(value) => consume(value)
None => report_invalid_number(text)
}
binary := value.to_radix(2)
decimal := value.to_string()int.from_string parses decimal text. to_radix(base) formats bases 2 through
36. float.from_string is also optional. Parsing returns None for malformed
or out-of-range input and never panics.
The low-level write_decimal methods write into caller-reserved byte storage.
They back Buffer.append_int and append_float; ordinary application code
should generally use to_string or the buffer helpers.
Choosing a type
Use int for ordinary counts, indexes expressed as signed application values,
and database integers. Use usize for layout sizes and pointer arithmetic.
Use fixed-width types at binary, network, FFI, and storage boundaries. Use
float unless a protocol specifically requires f32.
Binary floating point is not a decimal money type. Represent fixed-scale values with an integer minor unit or an application-defined decimal type.