How to create a basic calculator in Rust

Hello all, as some people may know, their is a lot of ways to create something in rust, just like any other programming language. However, as someone who is learning rust, I’ve noticed there is a lack of resources, and is why I’m posting it.

Requirements:

  • rust version 1.x and later.
  • VS code with the rust analyzer extension
  • Terminal for ease of use.

you can create your project directory using:

$ cargo new calc

After running their should be a folder named calc and inside a src folder containing the main.rs script. You can open it up in your IDE
of choice and it should look like this.

Now that we have that open we can make the following changes to the file.

You may notice we added std::io; to the script, this allows us to gather user input. We also added println!(); which we are going to use to tell the user what they can enter to get there operator. next we are going to add the following lines to the program. The following should all be in Fn main().

// create user input line

let mut input_line: String = String::new(); // the input line
io::stdin()
    .read_line(&mut input_line) // read the line 
    .expext("Failed to read line"); // because it does happen
let x: i32 = input_line.trim().parse().expect("Not an integer"); // store as variable

You might be asking yourself “does it really take all that code to just gather input?” Yes and that’s because Rust is a low level language. You are probably wondering what i32 is, i32 is a 32Bit signed integer type, it’s what tells rust how to use the variable.

// continuation of above
// create the if statements for the selected action
if x == 1 {
        {   
            println!("first number");
            let mut input_line: String = String::new();
            io::stdin()
                .read_line(&mut input_line)
                .expect("Failed to read line");
            let num1: f32 = input_line.trim().parse().expect("Input is not an integer");

            println!("second number");
            let mut input_line: String = String::new();
            io::stdin()
                .read_line(&mut input_line)
                .expect("Failed to read line");
            let num2: f32 = input_line.trim().parse().expect("Input is not an integer");
            
            let ans: f32 = num1 + num2; // calculate the answer as a floating point number
            println!("Answer {}", ans); // print out the answer
        }
}

if x == 2 {
        {   
            println!("first number");
            let mut input_line: String = String::new();
            io::stdin()
                .read_line(&mut input_line)
                .expect("Failed to read line");
            let num1: f32 = input_line.trim().parse().expect("Input is not an integer");

            println!("second number");
            let mut input_line: String = String::new();
            io::stdin()
                .read_line(&mut input_line)
                .expect("Failed to read line");
            let num2: f32 = input_line.trim().parse().expect("Input is not an integer");
            
            let ans: f32 = num1 - num2; // calculate the answer as a floating point number
            println!("Answer {}", ans); // print out the answer
        }
    }

    if x == 3 {
        {   
            println!("first number");
            let mut input_line: String = String::new();
            io::stdin()
                .read_line(&mut input_line)
                .expect("Failed to read line");
            let num1: f32 = input_line.trim().parse().expect("Input is not an integer");

            println!("second number");
            let mut input_line: String = String::new();
            io::stdin()
                .read_line(&mut input_line)
                .expect("Failed to read line");
            let num2: f32 = input_line.trim().parse().expect("Input is not an integer");
            
            let ans: f32 = num1 * num2; // calculate the answer as a floating point number
            println!("Answer {}", ans); // print out the answer
        }
    }

    if x == 4 {
        {   
            println!("first number");
            let mut input_line: String = String::new();
            io::stdin()
                .read_line(&mut input_line)
                .expect("Failed to read line");
            let num1: f32 = input_line.trim().parse().expect("Input is not an integer");

            println!("second number");
            let mut input_line: String = String::new();
            io::stdin()
                .read_line(&mut input_line)
                .expect("Failed to read line");
            let num2: f32 = input_line.trim().parse().expect("Input is not an integer");
            
            let ans: f32 = num1 / num2; // calculate the answer as a floating point number
            println!("Answer {}", ans); // print out the answer
        }
    }

}

Now we are pretty much done writing code, And I should explain how ownership works. ownership is like global and local scope, but without functions. To use ownership in rust you do the following.

// example of ownership
{
   println("hello world");
}

we also changed i32 over to f32 in case if our user needs to calculate something between two floating point numbers. With all that said I’m pretty much done, and feel free to ask questions.

This topic was automatically closed after 121 days. New replies are no longer allowed.