我创建了一个Rust宏,它可以扩展为函数声明。
macro_rules! build_fn
{
($name:tt) => {
pub fn $name(&self) -> Result<i32, Box<dyn Error>>
{
// <implementation>
Ok(0)
}
};
}
是否可以将其展开,以便宏可以采用可变参数?
例如
($name:tt, /*$variable_args ? */) => {
pub fn $name(&self, /*$variable_args ? */) -> Result<i32, Box<dyn Error>>
{
// ...
Ok(0)
}
};
}
游乐场
事实上,这是可能的。您需要将参数扩展为$field: $ty
:
use std::error::Error;
macro_rules! build_fn
{
($name:tt, $($v:ident: $t:ty),*) => {
pub fn $name(&self, $($v: $t),*)
{
let args = [$($v,)*];
println!("Args = {:?}", args);
}
};
}
struct MyStruct {}
impl MyStruct {
build_fn!(test_single_arg, x: i32);
build_fn!(test_multi_arg, x: i32, y: i32);
}
fn main() -> Result<(), Box<dyn Error>> {
let my_struct = MyStruct {};
my_struct.test_single_arg(10);
my_struct.test_multi_arg(1, 2);
Ok(())
}
游乐场链接:https://play.rust-lang.org/?version=nightly&mode=调试&edition=2021&gist=d798cc6a744b53af47dd8cf40fc3b115
由于@ranfdev的答案当前尚未编译,我修复了&简化&扩展了它以支持多种类型,现在它在Rust-stable:中运行
use std::fmt::Write;
macro_rules! build_vararg_fn {
($name:tt, $($v:tt: $t:ty),+) => {
fn $name($($v: $t),+) {
let mut msg = String::from("args: ");
$(
write!(msg, "{:?}, ", $v).unwrap();
)+
println!("{}", &msg[..msg.len()-2]);
}
}
}
fn main() {
build_vararg_fn!(test_single_arg, x: i32);
build_vararg_fn!(test_multi_arg, x: i32, y: i32);
build_vararg_fn!(test_multi_type, x: i32, y: i32, z: f64);
test_single_arg(10);
test_multi_arg(1, 2);
test_multi_type(1, 2, 3.14159);
}
输出:
args: 10
args: 1, 2
args: 1, 2, 3.14159
在PlayGround上看到它!