Originally published on php2rust.dev — part of the PHP to Rust course. View this lesson on the original site.
Build a small Rust program that classifies optional user inputs with pattern matching.
Exercise: Smart User Input Classifier
Goal
Practice these chapter 2 topics together:
- Variables and mutability
- Scalar and composite types
- Enums and matching
- Destructuring
- Loops
Option
Requirements
- Create a collection of optional numeric values that includes:
- present values (
Some(...)) - missing values (
None) - edge values such as
0, negative, and larger numbers
- present values (
- Loop through the collection and use
matchon eachOption. - For
Some(n), classify each number as:- positive / zero / negative
- even / odd
- For
None, print a clear "missing input" message. - Keep running counters for:
- total
Some - total
None - positive values
- even values
- total
- Print a final summary report with all counters.
Stretch goals
- Create an enum that represents classification results, then match on it.
- Add a match guard for a special case (for example, "large number").
- Store all per-item results and print them in a second pass.
What's next
Next: Smart user input classifier solution.
From PHP to Rust — Rust Syntax and Functions