Sling Academy
Home/Rust/Performing Basic Arithmetic Operations in Rust: Addition, Subtraction, Multiplication, Division

Performing Basic Arithmetic Operations in Rust: Addition, Subtraction, Multiplication, Division

Last updated: January 03, 2025

Rust is a systems programming language that is blazingly fast and memory-efficient. One of the fundamental aspects when you start learning any programming language is understanding how to perform basic arithmetic operations. Rust supports the primary arithmetic operations like addition, subtraction, multiplication, and division, which align with mathematical operations you're already familiar with.

Addition

Addition is the process of calculating the total or sum by combining two numbers. In Rust, you use the + operator to add two numbers together. Below is an example of the addition operation in Rust:

fn main() {
    let sum = 5 + 10;
    println!("The sum of 5 and 10 is: {}", sum);
}

In this example, two integers, 5 and 10, are added together using the + operator, and the result is stored in the variable sum. The println! macro is then used to print the result to the console.

Subtraction

Subtraction is the process of deducting one number from another. Rust facilitates this operation using the - operator. Here’s how you can perform subtraction in Rust:

fn main() {
    let difference = 12 - 3;
    println!("The difference when subtracting 3 from 12 is: {}", difference);
}

As shown here, 3 is subtracted from 12, and the resulting difference is stored in the difference variable.

Multiplication

Multiplication finds the product by multiplying two numbers. Rust uses the * operator to perform multiplication:

fn main() {
    let product = 4 * 3;
    println!("The product of 4 and 3 is: {}", product);
}

In this example, 4 is multiplied by 3 using the * operator, and the result is stored in the product variable.

Division

Division is the process of determining how many times one number is contained within another. The division is carried out by using the / operator in Rust:

fn main() {
    let quotient = 8 / 2;
    println!("The quotient when 8 is divided by 2 is: {}", quotient);
}

Here, the / operator divides 8 by 2, and the result is stored in the quotient variable.

Floating Point Division

It is also important to highlight floating-point division. When dividing two integers, Rust returns another integer. To obtain a floating-point result, you must use floating-point numbers:

fn main() {
    let quotient = 7.0 / 2.0;
    println!("The quotient when 7.0 is divided by 2.0 is: {}", quotient);
}

This example uses floating-point numbers to perform division, which gives you a more precise result.

Arithmetic with Variables

Arithmetic operations in Rust aren't limited to literals; you can just as easily perform these operations using variables:

fn main() {
    let a = 10;
    let b = 5;

    let add = a + b;
    let sub = a - b;
    let mult = a * b;
    let div = a / b;

    println!("Results of arithmetic operations:");
    println!("Addition: {}", add);
    println!("Subtraction: {}", sub);
    println!("Multiplication: {}", mult);
    println!("Division: {}", div);
}

Here variables a and b are used with the basic arithmetic operators to perform a series of computations.

Conclusion

Mastering these basic arithmetic operations is essential when getting start with Rust programming. These operations form the foundation upon which more complex operations and logic can be built. As you continue exploring Rust, you'll encounter various advanced mathematical techniques and libraries that allow you to leverage even more sophisticated arithmetic calculations.

Next Article: Exploring Rust’s Overflow Behavior: Wrapping, Saturating, and Panicking

Previous Article: Understanding Signed vs Unsigned Integers in Rust

Series: Math and Numbers in Rust

Rust

You May Also Like

  • E0557 in Rust: Feature Has Been Removed or Is Unavailable in the Stable Channel
  • Network Protocol Handling Concurrency in Rust with async/await
  • Using the anyhow and thiserror Crates for Better Rust Error Tests
  • Rust - Investigating partial moves when pattern matching on vector or HashMap elements
  • Rust - Handling nested or hierarchical HashMaps for complex data relationships
  • Rust - Combining multiple HashMaps by merging keys and values
  • Composing Functionality in Rust Through Multiple Trait Bounds
  • E0437 in Rust: Unexpected `#` in macro invocation or attribute
  • Integrating I/O and Networking in Rust’s Async Concurrency
  • E0178 in Rust: Conflicting implementations of the same trait for a type
  • Utilizing a Reactor Pattern in Rust for Event-Driven Architectures
  • Parallelizing CPU-Intensive Work with Rust’s rayon Crate
  • Managing WebSocket Connections in Rust for Real-Time Apps
  • Downloading Files in Rust via HTTP for CLI Tools
  • Mocking Network Calls in Rust Tests with the surf or reqwest Crates
  • Rust - Designing advanced concurrency abstractions using generic channels or locks
  • Managing code expansion in debug builds with heavy usage of generics in Rust
  • Implementing parse-from-string logic for generic numeric types in Rust
  • Rust.- Refining trait bounds at implementation time for more specialized behavior