Skip to content

Calculate with Whole Numbers

The Course
•
26 min study

Every value in Rust has a type, which describes what kind of value it is and what you can do with it. A whole number such as 95 is an integer. Rust has several integer types, named by whether they can be negative and how many bits of memory they use: i32 is a signed 32-bit integer (it can be negative), u8 is an unsigned 8-bit integer (0 to 255). When you write let minutes = 95;, Rust infers i32, its default for whole numbers. You can also write the type yourself, as in let minutes: i32 = 95;. That documents what you expect and lets the compiler flag a value that does not fit.

Why so many types? Each one has a fixed size, so Rust knows exactly how much memory a value needs and how fast arithmetic on it will be. The price is that every type has limits, and Rust takes those limits seriously, as you will see below.

Predict and run

Dividing two integers with / gives the whole quotient: how many times the divisor fits. The % operator gives the remainder: what is left over. Together they can split minutes into full hours and leftover minutes. Will 95 minutes produce one or two full hours? What remains?

Predict, then run Rust
fn main() {
    let minutes: i32 = 95;
    let hours = minutes / 60;
    let leftover = minutes % 60;
    println!("{hours} h {leftover} min");
}
Check your prediction
Text
1 h 35 min

60 fits into 95 once, with 35 left over. The division did not round 1.58 up to 2; integer division drops the fraction entirely. Try 59, 60 and 61 and predict each result first. At 60, the remainder is zero because 60 fits exactly once with nothing left.

Whole numbers and decimal numbers

A number with a decimal point, such as 7.0, has a floating-point type, f64 by default. Floating-point division keeps the fraction. Rust never mixes the two kinds silently:

Rust
fn main() {
    let minutes: i32 = 95;
    let rate: f64 = 1.5;
    let points = minutes * rate;
}

This fails with error[E0277]: cannot multiply i32 by f64. Other languages would quietly convert one side, and sometimes lose information doing it. Rust asks you to convert explicitly with as, so the conversion is visible in the code.

Compare integer and decimal division Rust
fn main() {
    println!("{}", 7 / 2);
    println!("{}", 7 % 2);
    println!("{}", 7.0 / 2.0);
    let minutes: i32 = 95;
    let hours = minutes as f64 / 60.0;
    println!("{hours:.2} hours");
}
Text
3
1
3.5
1.58 hours

7 / 2 is 3 with remainder 1, while 7.0 / 2.0 is 3.5. minutes as f64 turns 95 into 95.0 before dividing. Inside a placeholder, :.2 rounds the printed value to two decimal places; the stored number is unchanged. For counting minutes and sessions, integers are usually what you want, because they are exact.

Every integer type has limits

u8::MAX and i32::MAX name the largest value each type can hold.

Show the limits of two integer types Rust
fn main() {
    println!("u8 holds 0 to {}", u8::MAX);
    println!("i32 holds {} to {}", i32::MIN, i32::MAX);
}
Text
u8 holds 0 to 255
i32 holds -2147483648 to 2147483647

Going past a limit is called overflow. Rust treats it as a mistake, not as a quiet wrap-around. A value that cannot fit is rejected before the program runs:

Rust
fn main() {
    let minutes: u8 = 300;
    let count: u8 = 255;
    let more = count + 1;
}

The first line fails with error: literal out of range for u8, and the note explains that its range is 0..=255. The addition fails with error: this arithmetic operation will overflow. When the compiler cannot see the values in advance, the debug builds this course uses stop the program with a panic, an immediate crash with a message, rather than printing a wrong answer.

Pick i32 unless you have a reason

For everyday counts, i32 is a sensible default and leaves plenty of room. Reach for other types when the data demands it, such as u8 for a value that is truly 0 to 255.

Practice

Exercise

Your turn

A study timer reports 3725 seconds. Convert it into hours, minutes and seconds and print 1 h 2 min 5 s. Then change the input to 59 and expect 0 h 0 min 59 s.

Your working space Rust
fn main() {
    let seconds: i32 = 3725;
    // Calculate hours, minutes and the remaining seconds.
    println!("{seconds} s");
}
Hint 1: the idea

One hour holds 3600 seconds and one minute holds 60. Take out the full hours first, then split what remains into minutes and seconds.

Hint 2: a first step

let hours = seconds / 3600; counts the full hours, and seconds % 3600 is what is left for the minutes and seconds.

Worked solution
Run the solution Rust
fn main() {
    let seconds: i32 = 3725;
    let hours = seconds / 3600;
    let rest = seconds % 3600;
    let minutes = rest / 60;
    let leftover = rest % 60;
    println!("{hours} h {minutes} min {leftover} s");
}
Text
1 h 2 min 5 s

3725 holds one full hour (3600) with 125 seconds left, and 125 is 2 minutes with 5 seconds left. Each step divides what the previous step left over. With 59 seconds every quotient is 0, which shows the calculation handles values smaller than a minute. The checkpoint combines these calculations with bindings into one small receipt.

Was this article helpful?

Your response is saved on this device.