Macros in Rust are one of its most powerful features, enabling meta-programming and reducing repetitive code. A macro allows you to generate and include code fragments during compilation, making it an excellent tool for tasks like boilerplate elimination.
In this challenge, you'll create a macro that formats mathematical operations as human-readable strings. The macro should perform the calculation and present it in a clear, formatted way.
In this challenge, you will implement a macro named math_operations!
that:
The macro should format the output as: "{number} {operator} {number} = {result}"
Input Types:
Supported Operations:
+
)-
)*
)/
)Error Handling:
"Division by zero"
"Unsupported operator: {operator}"
If you're stuck, check out the hints below.
Use pattern matching to handle different operators
Remember to check for division by zero before performing division
The format!
macro is useful for creating the output string
Use expr
matchers in your macro for maximum flexibility, e.g.