I've created a Rust macro which expands to a function declaration.
macro_rules! build_fn
{
($name:tt) => {
pub fn $name(&self) -> Result<i32, Box<dyn Error>>
{
// <implementation>
Ok(0)
}
};
}
Is it possible to expand this so the macro can take variable parameters?
e.g.
($name:tt, /*$variable_args ? */) => {
pub fn $name(&self, /*$variable_args ? */) -> Result<i32, Box<dyn Error>>
{
// ...
Ok(0)
}
};
}
$($arg:expr),*.. but I can't quite get the syntax right. It makes me question if it's possible in this context but I confess that macros are certainly my weak point with Rust.$($arg:expr),*covers expressions, you need names and types for the arguments. You can use something like$($p:pat : $t:ty),* $(,)?for full cover, but if that's at the end, better to use$($args:tt)*.