I have a three different functions, of which I want to call one based on a macro argument. This argument should be pre-processed, which is why I thought I need to write it as expr
. However, I can't seem to find a way to distinguish different cases for an expr
in a macro. Here is my code:
fn func_100(){
println!("Func 100!");
}
fn func_200(){
println!("Func 200!");
}
fn func_300(){
println!("Func 300!");
}
macro_rules! generate_func_call {
(100) => {
func_100();
};
(200) => {
func_200();
};
(300) => {
func_300();
}
}
macro_rules! generate_func_call_wrapper {
($func: ident, $number: expr) => {
fn $func(){
println!("{:?}", $number / 100);
generate_func_call!($number);
}
};
}
generate_func_call_wrapper!(f1,100);
generate_func_call_wrapper!(f2,200);
generate_func_call_wrapper!(f3,300);
fn main(){
f1();
}
which generates the following compile time error:
generate_func_call!($number);
^^^^^^^ no rules expected this token in macro call
How can I fix this program so that calls a different function based on the $number
expression?