C.W.K.
Stream
Lesson 01 of 05 · published

Scalar Types & Overflow

~11 min · types, scalars, integers, overflow

Level 0Rust Curious
0 XP0/80 lessons0/19 achievements
0/100 XP to next level100 XP to go0% complete

Rust's scalar types are precise on purpose. Where many languages give you one fuzzy 'number,' Rust makes you say exactly what you mean — and that precision is a feature.

Integers

Integers come in signed (i8, i16, i32, i64, i128) and unsigned (u8u128) flavors, where the number is the bit width. There's also isize/usize, sized to the machine's pointer width — usize is what you index collections with. The default, when inference has nothing else to go on, is i32.

Floats, bool, char

f64 is the default float (full double precision); f32 when you need half the memory. bool is true/false. char is a full Unicode scalar — four bytes, so it holds an emoji or a Hangul syllable, not just ASCII. That's a real difference from C's one-byte char.

Overflow is defined, not undefined

Add past a type's max and Rust has a clear answer: debug builds panic (so you catch the bug in testing), release builds wrap (two's complement, for speed). Neither is the C-style undefined behavior that breeds security holes. When you want explicit behavior, say so: wrapping_add, checked_add (returns an Option), saturating_add (clamps to the max).

A debug panic on overflow is a gift. It's the compiler-as-mentor again: rather than silently producing a wrong number (C) or a slow bignum (Python), Rust stops and points at the exact overflow during testing. Ship with the explicit checked_* methods where overflow is plausible.

Inference plus annotation

You usually let the compiler infer the type, but annotate when it matters: let big: u64 = 1; or a suffix like 1u64. When you parse a string to a number, you must say which number — that's the most common place you'll annotate a scalar.

Code

Scalars, with overflow made explicit·rust
fn main() {
    let signed: i32 = -42;
    let unsigned: u8 = 255;       // max u8; 256 wouldn't fit
    let pi = 3.14159_f64;         // suffix picks the type
    let yes = true;
    let crab = '🦀';                 // a char is a full Unicode scalar

    // Explicit overflow handling — no surprises:
    let safe = unsigned.checked_add(1); // Some(256)? No: None (would overflow)
    println!("{signed} {pi} {yes} {crab} {safe:?}");
}

External links

Exercise

Declare a u8 holding 250, then add 10 to it three ways: plain + in a debug build (watch it panic), wrapping_add, and checked_add. Compare the three results and decide which you'd want in a checksum, a counter, and a game score.
Hint
Plain + panics in debug; wrapping_add rolls over to a small number; checked_add returns None. Checksums often want wrapping; scores usually want checked or saturating.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue
💛 by Ttoriwarm

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.