如何在 Rust 宏中扩展多个特征边界?

How are multiple trait bounds expanded in a rust macro?

我有一些 Rust 代码等同于下面的代码片段,但无法编译

trait A {}
trait B {}

macro_rules! implement {
    ( $type:ty ) => {
        struct C<T: $type> { // <-- Changing "$type" to "A + B" compiles
            t: T,
        }
    }   
}

implement!(A + B); 

fn main() {}

rustc 1.44.1 编译得到:

error: expected one of `!`, `(`, `,`, `=`, `>`, `?`, `for`, lifetime, or path, found `A + B`

$type 替换为 A + B 编译。

我的问题是为什么它不能按原样编译,如何更改才能这样做?

(注意:对 Rust 有点陌生,我相信有更简单的方法可以解决这个问题,任何建议都是有益的)

ty 宏参数类型有不同的用途:它是 type, not a bound。您可以为此使用多个令牌:

macro_rules! implement {
    ( $($token:tt)* ) => {
        struct C<T: $($token)*> {
            t: T,
        }
    }
}