I am going through Rust by Examples - Macros/DSL
The code shows:
macro_rules! calculate {
(eval $e:expr) => {{
{
let val: usize = $e; // Force types to be integers
println!("{} = {}", stringify!{$e}, val);
}
}};
}
fn main() {
calculate! {
eval 1 + 2 // hehehe `eval` is _not_ a Rust keyword!
}
calculate! {
eval (1 + 2) * (3 / 4)
}
}
Now I want my custom macro calculate
to return the calculated value. I tried with the following:
macro_rules! calculate {
(eval $e:expr) => {{
let val: usize = $e;
println!("{} = {}", stringify!{$e}, val);
val
}};
}
But it returns me error saying error[E0308]: mismatched types in val, expected type (), found type i32
.
How can I modify the above macro to return the calculated value? Thanks.