An open API service indexing awesome lists of open source software.

https://github.com/golgothaaksanti/cli-rust-calculator

A simple command-line calculator built with Rust. It supports basic arithmetic operations (+, -, *, /) and demonstrates user input handling, error handling, and clean control flow using Rustโ€™s Result type.
https://github.com/golgothaaksanti/cli-rust-calculator

calculator calculator-app cli rust rust-lang

Last synced: 4 months ago
JSON representation

A simple command-line calculator built with Rust. It supports basic arithmetic operations (+, -, *, /) and demonstrates user input handling, error handling, and clean control flow using Rustโ€™s Result type.

Awesome Lists containing this project

README

          

# ๐Ÿงฎ Rust CLI Calculator

A simple and interactive command-line calculator built with Rust. This project is great for beginners learning Rust fundamentals like error handling, input/output, pattern matching, and basic control flow.

---

## โœจ Features

- โœ… Supports addition (`+`), subtraction (`-`), multiplication (`*` or `x`), and division (`/`)
- ๐Ÿšซ Handles invalid input (e.g., text instead of numbers, divide by zero)
- ๐Ÿ” Keeps prompting until valid input is received
- ๐ŸŽ“ Great as a first Rust project

---

## ๐Ÿ“ฆ Installation

Make sure you have Rust installed. If not:

```bash
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

```

## Clone the repo and run

```bash
git clone https://github.com/GolgothaAksanti/cli-rust-calculator.git
cd cli-rust-calculator
cargo run

```

## Example Usage

```bash
๐Ÿงฎ Rust CLI Calculator
Enter the first number: 12
Enter the second number: 4
Enter an operator (+, -, *, /): /
Result: 3

```

handles errors gracefully:

```Bash
Enter the first number: hello
โŒ Invalid number. Please try again.

Enter the second number: -
โŒ Invalid number. Please try again.

Enter an operator (+, -, *, /): $
โŒ Invalid operator: $

```

## ๐Ÿง  What You'll Learn

- How to read from `stdin` and print to `stdout`

- Using `.trim()`, `.parse::()`, and handling `Result` types

- Writing clean and idiomatic Rust for command-line tools

## Project Structure

```css
cli-rust-calculator/
โ”œโ”€โ”€ src/
โ”‚ โ””โ”€โ”€ main.rs # Main calculator logic
โ”œโ”€โ”€ Cargo.toml # Project manifest

```

## ๐Ÿค Contributing

Pull requests and suggestions are welcome! For major changes, please open an issue first to discuss what youโ€™d like to change.

## ๐Ÿ’ก Bonus Ideas

- Add support for exponentiation (`^`)

- Support chaining multiple operations

- Build a history of calculations

- Add a GUI with `egui` or `gtk-rs`