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.
macro_rules! math_operations {
($a:expr, $op:expr, $b:expr) => {{
// Your code here
}};
}
#[macro_export]macro_rules! math_operations { // TODO: Implement the macro ($a:expr, $op:expr, $b:expr) => {{ let res=match $op { "+" => $a+$b, "-" => $a-$b, "*" => $a*$b, "/" => {if $b==0{panic!("Division by zero");}; $a/$b}, a => panic!("Unsupported operator: {}",a) }; format!("{} {} {} = {}",$a,$op,$b,res) }};}// Example usagepub fn main() { assert_eq!(math_operations!(4, "+", 2), "4 + 2 = 6"); assert_eq!(math_operations!(10, "-", 3), "10 - 3 = 7"); assert_eq!(math_operations!(6, "*", 4), "6 * 4 = 24"); assert_eq!(math_operations!(15, "/", 3), "15 / 3 = 5");}
#[macro_export]macro_rules! math_operations { ($arg1:expr, $op:expr, $arg2:expr) => { match $op { "+" => format!("{} + {} = {}", $arg1, $arg2, $arg1 + $arg2), "-" => format!("{} - {} = {}", $arg1, $arg2, $arg1 - $arg2), "*" => format!("{} * {} = {}", $arg1, $arg2, $arg1 * $arg2), "/" => { if $arg2 == 0 { panic!("Division by zero"); } format!("{} / {} = {}", $arg1, $arg2, $arg1 / $arg2) } _ => panic!("Unsupported operator"), } };}
#[macro_export]macro_rules! math_operations { // TODO: Implement the macro ($a:expr, $op:expr, $b:expr) => {{ match $op { "+" => { format!("{} {} {} = {}", $a, $op, $b, $a + $b) }, "-" => { format!("{} {} {} = {}", $a, $op, $b, $a - $b) }, "*" => { format!("{} {} {} = {}", $a, $op, $b, $a * $b) }, "/" => { if $b == 0 { panic!("Division by zero"); } format!("{} {} {} = {}", $a, $op, $b, $a / $b) }, _ => panic!("Unsupported operator: {}",$op) } }}}// Example usagepub fn main() { assert_eq!(math_operations!(4, "+", 2), "4 + 2 = 6"); assert_eq!(math_operations!(10, "-", 3), "10 - 3 = 7"); assert_eq!(math_operations!(6, "*", 4), "6 * 4 = 24"); assert_eq!(math_operations!(15, "/", 3), "15 / 3 = 5");}
#[macro_export]macro_rules! math_operations { ($a:expr, $op:expr, $b:expr) => ({ let result = match $op { "+" => { $a + $b }, "-" => { $a - $b }, "*" => { $a * $b }, "/" => { if $b == 0 { panic!("Division by zero"); } $a / $b }, _ => { panic!("Unsupported operator: {}", $op); } }; format!("{} {} {} = {}", $a, $op, $b, result) });}// Example usagepub fn main() { assert_eq!(math_operations!(4, "+", 2), "4 + 2 = 6"); assert_eq!(math_operations!(10, "-", 3), "10 - 3 = 7"); assert_eq!(math_operations!(6, "*", 4), "6 * 4 = 24"); assert_eq!(math_operations!(15, "/", 3), "15 / 3 = 5");}
#[macro_export]macro_rules! math_operations { ($a:expr, $op:expr, $b:expr) => {{ let result = match $op { "+" => $a + $b, "-" => $a - $b, "*" => $a * $b, "/" => { if $b == 0 { panic!("Division by zero"); } $a / $b } _ => panic!("Unsupported operator: {}", $op), }; format!("{} {} {} = {}", $a, $op, $b, result) }};}// Example usagepub fn main() { assert_eq!(math_operations!(4, "+", 2), "4 + 2 = 6"); assert_eq!(math_operations!(10, "-", 3), "10 - 3 = 7"); assert_eq!(math_operations!(6, "*", 4), "6 * 4 = 24"); assert_eq!(math_operations!(15, "/", 3), "15 / 3 = 5");}
#[macro_export]macro_rules! math_operations { ($a:expr, $op:tt, $b:expr) => {{ let result = match $op { "+" => $a + $b, "-" => $a - $b, "*" => $a * $b, "/" => { if ($b != 0) { $a / $b } else { panic!("Division by zero") } } _ => panic!("Unsupported operator: {}", $op), }; format!("{} {} {} = {}", $a, $op, $b, result) }};}
#[macro_export]macro_rules! math_operations { ($a:expr, $op:expr, $b:expr) => {{ let result = match $op { "+" => $a + $b, "-" => $a - $b, "*" => $a * $b, "/" => match $b { 0 => panic!("Division by zero"), _ => $a / $b, }, _ => panic!("Unsupported operator: {}", $op), }; format!("{} {} {} = {}", $a, $op, $b, result) }};}// Example usagepub fn main() { assert_eq!(math_operations!(4, "+", 2), "4 + 2 = 6"); assert_eq!(math_operations!(10, "-", 3), "10 - 3 = 7"); assert_eq!(math_operations!(6, "*", 4), "6 * 4 = 24"); assert_eq!(math_operations!(15, "/", 3), "15 / 3 = 5");}
#[macro_export]macro_rules! math_operations { ($a:expr, $op:expr ,$b:expr) => {{ let result = match $op { "+" => $a + $b, "-" => $a - $b, "*" => $a * $b, "/" => { match $b { 0 => panic!("Division by zero"), _ => $a / $b } } _ => panic!("Unsupported operator: {}", $op) }; format!("{} {} {} = {}", $a, $op, $b, result) }};}// Example usagepub fn main() { assert_eq!(math_operations!(4, "+", 2), "4 + 2 = 6"); assert_eq!(math_operations!(10, "-", 3), "10 - 3 = 7"); assert_eq!(math_operations!(6, "*", 4), "6 * 4 = 24"); assert_eq!(math_operations!(15, "/", 3), "15 / 3 = 5");}
#[macro_export]macro_rules! math_operations { ($a:expr, $op:expr, $b:expr) => {{ let val = match $op { "+" => $a + $b, "-" => $a - $b, "*" => $a * $b, "/" => $a / match $b { 0 => panic!("Division by zero"), _ => $b }, _ => panic!("Unsupported operator: {}", $op) }; format!("{} {} {} = {}", $a, $op, $b, val) }};}// Example usagepub fn main() { assert_eq!(math_operations!(4, "+", 2), "4 + 2 = 6"); assert_eq!(math_operations!(10, "-", 3), "10 - 3 = 7"); assert_eq!(math_operations!(6, "*", 4), "6 * 4 = 24"); assert_eq!(math_operations!(15, "/", 3), "15 / 3 = 5");}
#[macro_export]macro_rules! math_operations { ($a:expr, $op:expr, $b:expr) => {{ match $op { "+" => { format!("{} + {} = {}", $a, $b, $a + $b) } "-" => { format!("{} - {} = {}", $a, $b, $a - $b) } "*" => { format!("{} * {} = {}", $a, $b, $a * $b) } "/" => { if $b == 0 { panic!("Division by zero"); } else { format!("{} / {} = {}", $a, $b, $a / $b) } } _ => { panic!("Unsupported operator: {}", $op) } } }};}// Example usagepub fn main() { assert_eq!(math_operations!(4, "+", 2), "4 + 2 = 6"); assert_eq!(math_operations!(10, "-", 3), "10 - 3 = 7"); assert_eq!(math_operations!(6, "*", 4), "6 * 4 = 24"); assert_eq!(math_operations!(15, "/", 3), "15 / 3 = 5");}
#[macro_export]macro_rules! math_operations { ($a:expr, $op:expr, $b:expr) => {{ let result = match $op { "+" => $a + $b, "-" => $a - $b, "*" => $a * $b, "/" => { if $b == 0 { panic!("Division by zero"); } else { $a / $b } }, _ => panic!("Unsupported operator: {}", $op), }; format!("{} {} {} = {result}", $a, $op, $b) }};}// Example usagepub fn main() { assert_eq!(math_operations!(4, "+", 2), "4 + 2 = 6"); assert_eq!(math_operations!(10, "-", 3), "10 - 3 = 7"); assert_eq!(math_operations!(6, "*", 4), "6 * 4 = 24"); assert_eq!(math_operations!(15, "/", 3), "15 / 3 = 5");}
#[macro_export]macro_rules! math_operations { // TODO: Implement the macro ($a:expr, $op:expr, $b:expr) => { match $op { "+" => format!("{} + {} = {}", $a, $b, $a + $b), "-" => format!("{} - {} = {}", $a, $b, $a - $b), "*" => format!("{} * {} = {}", $a, $b, $a * $b), "/" => { if $b == 0 { panic!("Division by zero"); } else { format!("{} / {} = {}", $a, $b, $a / $b) } } _ => panic!("Unsupported operator: {}", $op), } };}// Example usagepub fn main() { assert_eq!(math_operations!(4, "+", 2), "4 + 2 = 6"); assert_eq!(math_operations!(10, "-", 3), "10 - 3 = 7"); assert_eq!(math_operations!(6, "*", 4), "6 * 4 = 24"); assert_eq!(math_operations!(15, "/", 3), "15 / 3 = 5");}
#[macro_export]macro_rules! math_operations { // TODO: Implement the macro ($a:expr, $op:expr, $b:expr) => {{ let result = match $op { "+" => $a + $b, "-" => $a - $b, "*" => $a * $b, "/" => { if $b == 0 { panic!("Division by zero"); } $a / $b }, _ => panic!("Unsupported operator: {}", $op), }; format!("{} {} {} = {}", $a, $op, $b, result) }};}// Example usagepub fn main() { assert_eq!(math_operations!(4, "+", 2), "4 + 2 = 6"); assert_eq!(math_operations!(10, "-", 3), "10 - 3 = 7"); assert_eq!(math_operations!(6, "*", 4), "6 * 4 = 24"); assert_eq!(math_operations!(15, "/", 3), "15 / 3 = 5");}
#[macro_export]macro_rules! math_operations { // TODO: Implement the macro ($a:expr, $b:literal, $c:expr) => { { let d = match $b { "+" => {$a + $c}, "-" => {$a - $c}, "*" => {$a * $c}, "/" => { if $c == 0 { panic!("Division by zero"); } $a / $c }, _ => { panic!("Unsupported operator: {}", $b); }, }; format!("{} {} {} = {}", $a, $b, $c, d) } }}// Example usagepub fn main() { assert_eq!(math_operations!(4, "+", 2), "4 + 2 = 6"); assert_eq!(math_operations!(10, "-", 3), "10 - 3 = 7"); assert_eq!(math_operations!(6, "*", 4), "6 * 4 = 24"); assert_eq!(math_operations!(15, "/", 3), "15 / 3 = 5");}