generics - 如何在 rust 宏中扩展多个特征边界?
问题描述
我有一些与下面的代码片段等效的 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 有点陌生,我相信有更简单的方法可以解决这个问题,任何建议都会有所帮助)
解决方案
推荐阅读
- http-headers - 如何在 Angular 13 中实现 max-age Cache 控制
- python - 在 Python API 调用中传递多个字符串
- c# - 如何读取位于 app 目录中的 settings.settings 文件
- javascript - NEXT JS req.body 未定义
- html - 仅使用 bash 从 HTML 文件中选择 n 个匹配的行
- javascript - 如何从 javascript .js 文件访问 QML Singleton?
- mysql - 位于其他表中的值之间的计数的 SQL 查询
- sql - 如何使用 SQL 子查询来限制父查询中返回的值的范围?
- reactjs - 如何消除异步formik / yup验证,它将在用户停止输入数据时进行验证?
- javascript - 传单:点击更改颜色