Start with the idea
Integers and floating-point values model different kinds of information. A signed integer such as i32 can represent negative values; an unsigned type such as u8 uses its bits for nonnegative values. usize is the natural type for an index or collection length because it matches the platform’s address width. f64 represents a broad range of fractional values, but most decimal fractions are approximations in binary, so exact equality is often the wrong test for a measured quantity. No numeric type is automatically better: start with what the domain means.
Read the code
let files: usize = 4;
let temperature: f64 = 21.5;
let mask: u8 = 0b1010;
println!(
"{files} {temperature} {mask}"
);The annotations document three different jobs.
Practice starter
let left: u32 = 4;
let right: i32 = 2;
let total = left + right;
Work through it
In the example, files is a length-like count, temperature is fractional, and mask is one byte of bit flags. Those annotations tell a reader why the three values should not be treated as interchangeable. Rust refuses to add unrelated numeric types without a conversion, which forces you to choose the result range and sign deliberately. Before adding a cast, ask whether negative values are possible, whether the result can exceed the smaller type, and whether rounding matters. Try an explicit conversion only after answering those questions; a broad as cast can hide a narrowing mistake rather than solve it.
Type inference still has rules. A bare integer literal commonly defaults to i32 when nothing else constrains it, and a bare floating literal commonly defaults to f64. An assignment, function parameter, or explicit annotation can provide the needed context. That is why the same literal may be accepted in one place and rejected in another. Read the expected and found types in the diagnostic instead of adding as automatically; a conversion should express a deliberate boundary between numeric domains.
An integer type sets both a range and a representation. u8 can hold whole numbers from 0 through 255; i8 includes negative values but has a smaller positive maximum. Types such as usize are used for collection indices and sizes and depend on the target pointer width. A floating-point type such as f64 represents many fractional values approximately, not as exact decimal money. Choose the type from the domain's valid values and operations, not from the smallest spelling.
Rust often infers a type from context, but it does not silently turn every numeric value into whatever a later expression wants. Adding a u8 to an i32 requires an explicit decision about conversion. A cast with as has defined conversion behavior, but it can truncate or wrap values; use TryFrom when out-of-range input should produce an error. Division of two integers discards the fractional part. Overflow behavior also depends on operation and build context, so use checked or saturating operations when the domain needs an explicit policy.
Try computing an average from two whole numbers. Predict the result of integer division, then convert deliberately to floating point and compare. Next parse a value that may exceed u8 and handle the failed conversion instead of assuming it fits. Write a one-sentence reason for each chosen type. This is the real skill: using a type to state a valid range and deciding what happens at its boundaries.
A lossy integer as cast is not automatically memory-unsafe and does not require an unsafe block. For example, 300_i32 as u8 is defined to produce 44. It can still be the wrong domain result. Use u8::try_from(300_i32) when an out-of-range value must be rejected, and handle the returned error. Keep a logical data-loss error separate from Rust memory unsafety.
Try it
Choose types for a file count, a temperature, and one byte of flags. Explain the range or precision each choice needs; then try mixing two types and decide which explicit conversion preserves the intended result.
What success looks like: The values compile with types that match their jobs. Explain every conversion and show a narrowing cast that is defined Rust behavior but loses information required by the domain.
Check your understanding
Why is the addition rejected?
Reveal the answer
- Rust cannot add integers
- u32 and i32 are distinct types
- Only usize supports addition
Convert deliberately only after deciding which range and sign rule the result needs.
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: Numbers (courseId "basics", lessonId "number-types") Link: https://crabrix.com/learn/basics/number-types/ Goal: Numeric conversions are explicit because ranges and precision differ. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=basics/number-types) 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.