Since 2009, we have been utilizing our extensive expertise in blockchain technologies to help businesses, both large and small, maximize their efficiency.
Explore More
With more than 400+ experts, Oodles comprises a fantastic resource of business knowledge that spans multiple industries. Whatever the circumstances, we keep to our obligations.
Explore More
At Oodles, we help our clients work with a human understanding but at superhuman speed something that others can't. They thus advance and maintain their lead
31st October 2023
9 min read
Associate Consultant - Frontend Development
In this blog post, you will learn how to create a calculator using Rust in mobile app development.
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.
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
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.
We would love to
hear from you!
Innovate with confidence!