facebook

Contact Us

An Explainer to Calculator Development using Rust

Calender

31st October 2023

Clock

9 min read

Author
Pratham Jain

Associate Consultant - Frontend Development

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.

Table of Content

    Author Pratham Jain

    Pratham is an aspiring Frontend Developer with a year of industry experience. He possesses a strong command over essential technologies like JavaScript, HTML5, CSS, and specializes in ReactJS. Pratham has successfully delivered multiple projects, including contributions to the Bluechain exchange platform and various others. His dedication to excellence drives him to continuously acquire new knowledge and enhance his skills. By staying updated with the latest advancements, it ensures that he delivers exceptional results in his projects.

    Associate Consultant - Frontend Development

    bg bg

    What's Trending in Tech

    bg

    Our Offices

    India

    INDIA

    DG-18-009, Tower B,
    Emaar Digital Greens, Sector 61,
    Gurugram, Haryana
    122011.
    Unit- 117-120, First Floor,
    Welldone Tech Park,
    Sector 48, Sohna road,
    Gurugram, Haryana
    122018.
    USA

    USA

    30N, Gloud St STR E, Sheridan, Wyoming (USA) - 82801
    Singapore

    SINGAPORE

    10 Anson Road, #13-09, International Plaza Singapore 079903.

    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.