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

  1. Create a collection of optional numeric values that includes:
    • present values (Some(...))
    • missing values (None)
    • edge values such as 0, negative, and larger numbers
  2. Loop through the collection and use match on each Option.
  3. For Some(n), classify each number as:
    • positive / zero / negative
    • even / odd
  4. For None, print a clear "missing input" message.
  5. Keep running counters for:
    • total Some
    • total None
    • positive values
    • even values
  6. 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