Generics 如何在rust宏中扩展多个特征边界?
我有一些与下面的代码片段相当的锈迹代码,它无法编译Generics 如何在rust宏中扩展多个特征边界?,generics,rust,compiler-errors,compilation,macros,Generics,Rust,Compiler Errors,Compilation,Macros,我有一些与下面的代码片段相当的锈迹代码,它无法编译 trait A {} trait B {} macro_rules! implement { ( $type:ty ) => { struct C<T: $type> { // <-- Changing "$type" to "A + B" compiles t: T, } } } implement
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() {}
将$type
替换为A+B
编译时
我的问题是,为什么它不能按原样编译,如何才能将其更改为按原样编译
(注意:有点生疏,我相信有更简单的方法来解决这个问题,任何建议都是有益的)
ty
宏参数类型有不同的用途:它是a,而不是a。您可以为此使用多个令牌:
macro\u规则!实施{
($($令牌:tt)*)=>{
结构C{
t:t,
}
}
}
另请参见:
error: expected one of `!`, `(`, `,`, `=`, `>`, `?`, `for`, lifetime, or path, found `A + B`