ProgramGuru

Numbers, division and conversions

Numbers, division and conversions

A report total can be a whole number while its average includes a fraction. Rust offers signed integers such as i32, unsigned integers such as u32, and floating-point numbers such as f64. The type matters because arithmetic follows its operands rather than guessing the intended school calculation.

What you’ll learn

  • Calculate a fractional average with deliberate numeric conversion.
  • Recognize integer truncation and checked overflow.

Numbers, division and conversions in action

  1. Total: 155

    The integer result is 77.

    Total: 155
  2. Integer result: 77

    The converted calculation produces 77.5.

    Integer result: 77
  3. Fractional result: 77.5

    A u8 can hold at most 255, so checked_add reports None for the overflowing addition.

    Fractional result: 77.5
The integer result is 77. The converted calculation produces 77.5. A u8 can hold at most 255, so checked_add reports None for the overflowing addition. The suffix identifies the integer type; {:?} requests debug formatting.

Numbers, division and conversions explained

Dividing integers discards the fractional part toward zero. Convert before division when you need a fractional average. f64::from performs the supported conversion here. Checked arithmetic returns an Option instead of relying on overflow behavior. The as cast can lose information; it is not a validation function.

Option represents either Some(value) for a present result or None for no result. The checked addition returns None when no valid u8 sum exists. We will use the same explicit-absence idea for missing marks and collection lookups.

Work through the Rust program

Save the complete source as main.rs in the Rust environment. No standard input is needed.

Rust 2024 · complete program

fn main() {
  let total: i32 = 155;
  println!("{}", total / 2);
  println!("{}", f64::from(total) / 2.0);
  println!("{:?}", 255_u8.checked_add(1));
}

Expected output

77
77.5
None

Follow the execution

The integer result is 77. The converted calculation produces 77.5. A u8 can hold at most 255, so checked_add reports None for the overflowing addition. The suffix identifies the integer type; {:?} requests debug formatting.

Try the complete program in the Rust compiler. For tests, Cargo commands and local files, use the specified local environment.

Try a small variation

Your task: Why does the first division not produce 77.5?

Rust 2024 · complete program

fn main() {
  let total: i32 = 155;
  println!("{}", total / 2);
  println!("{}", f64::from(total) / 2.0);
  println!("{:?}", 255_u8.checked_add(1));
}

Worked answer

Both operands use integer arithmetic. Convert a suitable operand before dividing when the report requires a fractional result.

Mistakes and limits to recognize

Ordinary integer overflow can panic with checks enabled and wrap when checks are disabled. Use checked, saturating or wrapping operations deliberately. Floating-point values approximate many decimal quantities; they are not exact money arithmetic.

Check your understanding

Why does the first division not produce 77.5?
Choose one answer. You can try again.

Answer and explanation

Both operands use integer arithmetic.

Both operands use integer arithmetic. Convert a suitable operand before dividing when the report requires a fractional result.

Take the idea into your next program

Both operands use integer arithmetic. Convert a suitable operand before dividing when the report requires a fractional result. Ordinary integer overflow can panic with checks enabled and wrap when checks are disabled.

Further reading: the official Rust Book on numbers, division and conversions.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Operators and Boolean conditions →