An Explainer to Calculator Development using Rust

Posted By : Pratham

Oct 30, 2023

In this blog post, you will learn how to create a calculator using Rust in mobile app development

 

Calculator Development using Rust

 

To create a calculator in Rust, first, we need to install Rust and cargo. Please refer to the link https://www.rust-lang.org/tools/install for installation according to your operating system.

 

Then you need to create a rust project using the command "cargo new <your project name>.

 

Check It Out: Getting Started with Low-Code/No-Code App Development

 

Let's start creating a calculator in Rust.

 

Starting with the add function. In this function, we will accept two arguments of integer 32 bit and the return type will be the same as integer 32 bit. Then, we will add two numbers and simply return it.

 

// Function to add two numbers

 

fn add(a: i32, b: i32) -> i32 {
    a + b
}

 

Coming to the next subtract function, in this function, we will accept two arguments of integer 32 bit and the return type will be the same as integer 32 bit. Then we will just subtract two numbers and simply return it.

 

// Function to subtract two numbers

 

fn subtract(a: i32, b: i32) -> i32 {
    a - b
}

 

Next, multiply function in which we will accept two arguments of integer 32-bit and the return type will be the same as integer 32-bit. Then we will multiply two numbers and simply return them.

 

// Function to multiply two numbers

 

fn multiply(a: i32, b: i32) -> i32 {
    a * b
}

 

Last is the divide function, in this function, we will accept two arguments of integer 32 bit and the return type will be a Result enum. In this case, the Result enum will either return a floating 32-bit integer or return a static string. Then we will just divide two numbers and simply return them.

 

// Function to divide two numbers

 

fn divide(a: i32, b: i32) -> Result<f32, &'static str> {
    if b == 0 {
        Err("Division by zero is not allowed.")
    } else {
        Ok(a as f32 / b as f32)
    }
}

 

Here is the main file, we are calling all the functions created above in this file and can check our calculator is working fine.

 

To run this file, we need to execute a "cargo run" in the terminal.

 

fn main() {
    let num1 = 10;
    let num2 = 5;

    println!("Sum: {}", add(num1, num2));
    println!("Difference: {}", subtract(num1, num2));
    println!("Product: {}", multiply(num1, num2));

    match divide(num1, num2) {
        Ok(result) => println!("Quotient: {}", result),
        Err(error) => println!("Error: {}", error),
    }
}

 

Suggested Post: Mobile App Development Trends 2023 


Here, match is used to check the divide function returned value. If it provides a value then it will print the Quotient part with the resulted value else it will print the error with the error value.

 

If you are interested in app development, then connect with our app developers to get started.

Leave a

Comment

Name is required

Invalid Name

Comment is required

Recaptcha is required.

blog-detail

November 22, 2024 at 10:49 pm

Your comment is awaiting moderation.

By using this site, you allow our use of cookies. For more information on the cookies we use and how to delete or block them, please read our cookie notice.

Chat with Us
Telegram Button
Youtube Button

Contact Us

Oodles | Blockchain Development Company

Name is required

Please enter a valid Name

Please enter a valid Phone Number

Please remove URL from text