Calculator in Rust

Build an expression evaluator in Rust, using postfix notation to compute the result.

Calculator in Rust cover

What You'll Build

We'll build a fully functional command-line calculator from the ground up in Rust. Starting with a minimal REPL, we progressively add a tokenizer that splits expressions into meaningful tokens, implement the Shunting Yard algorithm to convert infix notation into Reverse Polish Notation, and assemble a stack-based evaluator that computes the final result.

Along the way we'll practice idiomatic Rust patterns: custom error types with Display, the Result/? pipeline, newtype wrappers, trait implementations (FromStr, Add, Sub, Mul, Div), and test-driven development. By the end, our calculator handles parenthesized expressions with correct operator precedence, decimal numbers, and clear error messages for invalid input.

Learning Objectives

  • Build an interactive REPL with clean input/output handling

  • Implement a tokenizer for arithmetic expressions

  • Convert infix notation to RPN using the Shunting Yard algorithm

  • Evaluate RPN expressions with a stack

  • Design a robust error-handling pipeline with custom error types

  • Apply the newtype pattern together with trait implementations

Prerequisites

  • Basic Rust syntax (functions, enums, structs)

  • Familiarity with pattern matching and the Result type

  • Understanding of basic arithmetic and operator precedence

Course curriculum

1 Interactive shell

  • Project skeleton
  • Read loop
  • Shell commands

2 The calculate function and the tokenizer

  • Entry point
  • The tokenizer skeleton
  • The CalcError error type

3 Parsing numbers

  • The number collector
  • Tests and fractional numbers

4 Tokens

  • The Token type
  • Tokenization test

5 Operators

  • The operator token
  • The Operator type
  • The unary minus

6 Parentheses

  • Parenthesis tokens
  • Tracking context

7 Shunting yard

  • The to_rpn skeleton
  • Operator precedence
  • Closing parentheses
  • Final parenthesis checks

8 Stack-based evaluation

  • The eval_rpn skeleton
  • Operations on the stack
  • Evaluation errors
  • Unary minus in the evaluator

Technologies

Rust Calculator Shunting Yard Reverse Polish Notation Tokenizer REPL Error Handling Newtype Pattern

FAQ

Is this a video course?

No. You implement the project yourself, step by step, right in your browser — with a full reference solution for every step when you get stuck.

Do I need to install anything?

No. The playground runs entirely in your browser — no toolchain or local setup required.

Can I try it before subscribing?

Yes. Preview steps are available without a subscription so you can see the format and quality first.

What do I get with full access?

Every step unlocked, the Workbench app, a snapshot for each step, jump-to-any-step navigation, and all new playground releases.

Does it work with an AI agent like Claude or Codex?

Yes. You can work through a playground solo or alongside an AI agent.

Is there reference code?

Yes. Every step has a complete reference solution you can compare against.