I've (naively) tried this, but it doesn't print anything to the screen:
macro_rules! foo {
($suffix:tt, $arg:expr) => {
concat!("foo", $suffix, "(", $arg, ")");
};
}
fn foo_i32(x: i32) {
println!("i32 {}", x);
}
fn foo_bool(x: bool) {
println!("bool {}", x);
}
fn main() {
foo!("bool", true);
foo!("i32", 1);
}
Yes, and no.
First of,
concat!
generates a string, so your code is essentially the same as if you wrote:which is a no-op.
To generate Rust code, the macro does not need to involve strings at all:
which you could call as
foo!(foo_bool, true);
.If however you want to construct the name
foo_bool
fromfoo
andbool
, you need to useconcat_idents
, which is currently unstable and unlikely to get stable any time soon (because it causes some hygiene issues):