我可以使用宏而不是特性來構建結構和枚舉。這是一個錯誤還是關於我缺少的特質如何工作?下面是未能建立一個簡單的例子:Do Rust宏在特徵定義內工作嗎?
macro_rules! fun{
() => { fn hello(); }
}
macro_rules! full_fun{
() => { fn hello(){} }
}
// Fails with:
// <anon>:13:8: 13:11 error: expected one of `const`, `extern`, `fn`, `type`, or `unsafe`, found `fun`
// <anon>:13 fun!();
macro_rules! trait_macro{
($name:ident) => {
pub trait $name {
fun!();
}
};
}
macro_rules! struct_macro{
($name:ident) => {
pub struct $name;
impl $name {
full_fun!();
}
};
}
// I can add functions to a Impl
struct_macro!{Monster}
// But I cannot add functions to a trait
trait_macro!{Monster}
fn main() {
}
這現在已經實現請參閱[Issue34183](https://github.com/rust-lang/rust/issues/34183) –