Subtracting Numbers in Rust

Subtracting Numbers in Rust

Subtraction is one of the first math operations we learn, and it plays a big role in programming as well. From calculating remaining balance in a wallet to tracking score differences in a game, subtraction helps programs understand change. When you subtract numbers in Rust, you are doing something very familiar, but in a way that follows Rust’s clear and safe rules.

Rust may look strict at first, but that strictness is actually helpful, especially for beginners. It forces you to be clear about your data and your intentions. In this guide, you will learn how to subtract numbers in Rust step by step, using simple examples and plain English explanations. By the end, subtraction in Rust will feel natural and easy.

Program 1: Subtracting two integers in Rust

This first program shows the simplest form of subtraction using whole numbers. It uses predefined values so you can focus only on understanding the subtraction itself.

fn main() {

    let total_apples: i32 = 20;
    let eaten_apples: i32 = 7;

    let remaining_apples = total_apples - eaten_apples;

    println!("Remaining apples: {}", remaining_apples);

}

In this program, two integer variables are created using the i32 type. Rust subtracts the second value from the first using the minus sign and stores the result. This example is useful because it shows that subtraction in Rust looks almost the same as regular math, which makes it easy for beginners to understand.

Program 2: Subtracting floating-point numbers

Sometimes subtraction involves decimal values, such as prices or measurements. This program shows how Rust handles subtraction with floating-point numbers.

fn main() {

    let account_balance: f64 = 150.75;
    let withdrawal_amount: f64 = 45.25;

    let new_balance = account_balance - withdrawal_amount;

    println!("New balance: {}", new_balance);

}

Here, the f64 type is used for numbers with decimal points. Rust performs the subtraction accurately and prints the result. This example helps beginners see how Rust works with real-world values that are not whole numbers.

Program 3: Subtracting mixed number types

In real programs, you may need to subtract a whole number from a decimal number. Rust requires you to make the types match before doing this.

fn main() {

    let total_hours: f64 = 40.0;
    let worked_hours: i32 = 28;

    let remaining_hours = total_hours - worked_hours as f64;

    println!("Remaining hours: {}", remaining_hours);

}

The integer value is converted into a floating-point number using as f64 before subtraction. This keeps Rust safe and clear about what is happening. Beginners learn from this example that Rust avoids guessing and expects you to be explicit.

Program 4: Subtracting numbers using a function

As programs grow, functions help keep code clean and reusable. This program shows how to subtract numbers inside a function.

fn subtract_numbers(first: i32, second: i32) -> i32 {
    first - second
}

fn main() {

    let result = subtract_numbers(50, 18);
    println!("The result is: {}", result);

}

The function takes two numbers and returns their difference. This approach makes the code easier to manage and reuse. Beginners can apply this idea when building larger Rust programs with repeated calculations.

Program 5: Subtracting numbers from user input

This program allows the user to enter numbers, making the example more practical and interactive.

use std::io;

fn main() {

    let mut input_one = String::new();
    let mut input_two = String::new();

    println!("Enter the first number:");
    io::stdin().read_line(&mut input_one).unwrap();

    println!("Enter the second number:");
    io::stdin().read_line(&mut input_two).unwrap();

    let first_number: i32 = input_one.trim().parse().unwrap();
    let second_number: i32 = input_two.trim().parse().unwrap();

    let difference = first_number - second_number;

    println!("The difference is: {}", difference);

}

The program reads user input as text and converts it into numbers before subtracting. Rust requires this step to avoid mistakes and keep programs safe. This example shows beginners how real applications work with user data.

Program 6: Subtracting large numbers safely

Rust is often used in systems programming where large numbers matter. This example shows subtraction using a larger integer type.

fn main() {

    let total_population: i64 = 5_000_000_000;
    let migrated_population: i64 = 1_200_000_000;

    let remaining_population = total_population - migrated_population;

    println!("Remaining population: {}", remaining_population);

}

Using i64 allows Rust to handle very large values safely. This program helps beginners understand why choosing the right number type is important in Rust.

Frequently Asked Questions (FAQ)

This section answers common beginner questions about subtracting numbers in Rust.

Q1. Why does Rust require number types for subtraction?
Rust uses number types to prevent errors and make programs predictable and safe.

Q2. Can Rust subtract decimals and integers together?
Yes, but you must convert one type so both values match before subtraction.

Q3. What happens if subtraction causes a negative result?
Rust allows negative values as long as the number type supports them.

Q4. Is subtraction slower in Rust compared to other languages?
No, Rust is very fast and often as fast as low-level languages like C.

Q5. Do I always need functions for subtraction?
No, but functions make code cleaner and easier to reuse in larger programs.

Conclusion

Subtracting numbers in Rust is simple once you understand the basics. You have seen how subtraction works with integers, floating-point numbers, mixed types, functions, user input, and large values. Each example shows Rust’s focus on clarity and safety while still keeping the code readable.

The best way to get comfortable with Rust subtraction is to practice. Try changing values, using different number types, or building small programs of your own. With regular practice, these simple operations will become second nature and help you move forward confidently in your Rust programming journey.

Scroll to Top