Tiny project: Calculator
A fun first project
Let’s get right to it:
fn add(a: i32, b: i32) -> i32 {
a + b
}
fn main() {
let result = add(5, 10);
println!("Sum: {}", result);
}
Project Requirements
- Description: Making a basic calculator in Rust is a great first project. Why? Because it's simple and teaches the fundamental syntax without overwhelming complexity.
- Problem: New programmers often feel overwhelmed when attempting complex projects. Starting with a basic calculator provides a gentle introduction to fundamental programming concepts.
- Solution: Implement functions for basic arithmetic operations: addition, subtraction, multiplication, and division. This small project is manageable and educational.
- Fun Aspect: It helps new users grasp Rust's type system, function returns, and basic control flow. For instance, you can quickly see how integers work and get familiar with defining and calling functions.
Here’s an example of a simple addition function in Rust:
fn add(a: i32, b: i32) -> i32 {
a + b
}
fn main() {
let result = add(5, 10);
println!("Sum: {}", result);
}
Exploring the problem
Now, let's build on that by handling more operations. Using Rust’s control flow, we can direct our calculator to perform addition, subtraction, multiplication, or division based on user input. We'll use match statements to manage this control flow.
Here's a more fleshed-out version:
fn add(a: i32, b: i32) -> i32 {
a + b
}
fn subtract(a: i32, b: i32) -> i32 {
a - b
}
fn multiply(a: i32, b: i32) -> i32 {
a * b
}
fn divide(a: i32, b: i32) -> Option<i32> {
if b != 0 {
Some(a / b)
} else {
None
}
}
fn main() {
let operator = "+";
let a = 8;
let b = 2;
let result = match operator {
"+" => add(a, b).to_string(),
"-" => subtract(a, b).to_string(),
"*" => multiply(a, b).to_string(),
"/" => match divide(a, b) {
Some(result) => result.to_string(),
None => "Cannot divide by zero".to_string(),
},
_ => "Invalid operator".to_string(),
};
println!("Result: {}", result);
}
In this snippet:
- We defined four functions: `add`, `subtract`, `multiply`, and `divide`.
- The `divide` function returns an `Option<i32>` to handle division by zero gracefully.
- A match statement directs our calculator to the appropriate function based on the input operator.
Including error handling in our divide function elevates our tiny calculator, making it more robust. By handling potential errors like division by zero, we learn to use Rust's `Option` and `Result` types effectively.
Extending the Project
To make this calculator more interactive, you can extend it to handle user inputs via the command line. The `std::io` module provides functions to read user inputs, making the program more dynamic.
Here’s how you can extend our calculator to take user input:
use std::io;
fn main() {
println!("Enter first number: ");
let mut num1 = String::new();
io::stdin().read_line(&mut num1).expect("Failed to read line");
let num1: i32 = num1.trim().parse().expect("Please type a number");
println!("Enter second number: ");
let mut num2 = String::new();
io::stdin().read_line(&mut num2).expect("Failed to read line");
let num2: i32 = num2.trim().parse().expect("Please type a number");
println!("Enter operator (+, -, *, /): ");
let mut operator = String::new();
io::stdin().read_line(&mut operator).expect("Failed to read line");
let operator = operator.trim();
let result = match operator {
"+" => add(num1, num2).to_string(),
"-" => subtract(num1, num2).to_string(),
"*" => multiply(num1, num2).to_string(),
"/" => match divide(num1, num2) {
Some(result) => result.to_string(),
None => "Cannot divide by zero".to_string(),
},
_ => "Invalid operator".to_string(),
};
println!("Result: {}", result);
}
Here’s what we added:
- We prompt the user to enter two numbers and an operator.
- We retrieve these inputs and process them using our original functions and match statement.
This extension helps anyone learn how to interact with users, manage basic program flow, and handle potential input errors.
By starting with a small yet complete project like a tiny calculator, new programmers build confidence and a solid understanding of Rust's core concepts. It's simple, educational, and surprisingly engaging.


