Originally published on php2rust.dev — part of the PHP to Rust course. View this lesson on the original site.
You’ve wired up the calculator in src/main.rs. Now run it from your project folder (the same directory that contains Cargo.toml).
Compile and run
In a terminal:
cargo run
cargo run does two things in one step: it builds your crate (if anything changed) and then runs the resulting program. The first run may take a few seconds while dependencies and your code compile; later runs are faster when nothing needs recompiling.
You’ll see the welcome message, then the prompts for two numbers and an operator, same flow as in the PHP version. Try a few combinations (+, -, *, /) and confirm the results look right.
While you’re testing
- Invalid numbers, If you type plain text instead of a number, the program should ask again until the input parses as a float. That’s the
get_numberloop doing its job. - Division by zero, Choosing
/with a second operand of0should show the error message and not print a numeric result. - Wrong operator, Anything other than
+,-,*, or/should trigger the “invalid operation” path.
If something doesn’t match that behaviour, double-check that src/main.rs matches the Step 2 lesson (or the companion repo, if you’re using it).
Where you are in the project
You’re still in debug mode, the default for cargo run. Cargo puts the built binary under target/debug/ inside your project. You don’t need to run that file by hand; cargo run is enough for day-to-day work. Later you’ll see cargo build --release for smaller, faster production binaries.
Stopping the program: press Ctrl+C in the terminal if you need to exit while it’s waiting for input.
What's next
Next: What you learned.
From PHP to Rust — Getting Started with Rust