Start with the idea
There is no implicit numeric promotion: adding a u8 to a u32 is a compile error until you convert. An ordinary integer operation can panic when overflow checks are enabled. Common build profiles have different defaults, and those defaults are configurable. Use named checked, wrapping or saturating methods when the application requires a particular overflow policy.
Read the code
let a: u8 = 250;
println!("{:?}", a.checked_add(10)); // None
println!("{}", a.wrapping_add(10)); // 4
println!("{}", a.saturating_add(10));// 255
let big = 300_i32;
let narrowed = big as u8; // truncates on purpose
println!("{narrowed}");Say what should happen at the boundary instead of hoping.
Practice starter
let total: u8 = 200;
let bonus: u8 = 100;
let result = total.wrapping_add(bonus);
println!("{result}"); // 44: this counter wrapped
Work through it
The example shows why an overflow policy must be explicit. A u8 can hold values from 0 through 255, so 250 + 10 does not fit. checked_add returns None rather than a number; wrapping_add cycles around to 4; saturating_add clamps at 255. These are three different product decisions. A count of completed tasks probably should not wrap from 250 to 4, while a circular sequence number may intentionally do so. Pick the behavior from the domain before choosing the method.
The later as cast illustrates another boundary. Converting 300_i32 to u8 with as keeps only the low eight bits, yielding 44. This is not a harmless way to make two types agree. If narrowing should fail when a value does not fit, use a checked conversion such as u8::try_from(big) and handle its Result. If the source is known to fit, record why that invariant holds. Rust's refusal to silently promote mixed numeric operands gives you a chance to make that reasoning visible.
Overflow checking can be configured, so avoid depending on a debug-versus-release difference as part of program logic. The named arithmetic methods communicate behavior reliably in either build. As an exercise, write the expected output for all three additions before running the example, then explain which one would be appropriate for a byte-sized inventory counter that must report an error when it exceeds its limit.
Do not assume all numeric conversions happen automatically. Rust requires deliberate casts where types differ, and a cast can truncate or change meaning. Start with concrete types, then inspect the compiler diagnostic before changing them to make an expression compile.
Operators are short names for operations, but their result still has a type and a range. Integer division truncates toward zero; % returns a remainder. A comparison returns bool, not a number. If several operators appear in one expression, add parentheses while learning so you can see exactly which operation happens first. The compiler can catch incompatible operands, but it cannot tell you whether the formula itself matches the problem.
Try calculating the cost of three items after a discount. Write the steps as separate bindings, print each intermediate value, and compare with a single expression. Then test a boundary such as zero divisor or a value near the maximum of your integer type. Decide whether your program should reject, clamp, or explicitly handle that case. Converting between numeric types is a policy decision: a cast that silently loses information may be wrong even if it compiles.
Try it
Return the number of full groups and the leftover count. The group size is always positive. In the Crabrix app, open the compiler lab and complete the supplied function in solution.rs. The reading question is a separate exercise.
What success looks like: The local compiler accepts the changed solution and the separate verification harness finishes all normal and boundary cases. Printing a success message alone does not complete this lab.
Check your understanding
Which call returns None instead of panicking or wrapping?
Reveal the answer
- wrapping_add
- checked_add
- saturating_add
checked_* returns Option, so the caller decides what an overflow means.
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Operators & Casting (courseId "basics", lessonId "operators") Link: https://crabrix.com/learn/basics/operators/ Goal: Numeric operands need compatible types; choose the overflow policy explicitly. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=basics/operators) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.