【发布时间】:2018-03-24 00:37:18
【问题描述】:
我正在尝试创建一个宏,该宏使用 P::Child from struct P where P: Parent<Child = T> 初始化结构 T。
macro_rules! init {
($t:tt, { $( $k:ident => $v:expr ),* }) => {
<$t as Parent>::Child {
$( $k: $v ),*
}
};
}
此宏将 props 作为映射传递给结构的给定构造函数。展开后,它看起来像这样:
#[derive(Debug)]
struct Apple {
a: i32
}
trait Parent {
type Child;
}
struct Mango;
impl Parent for Mango {
type Child = Apple;
}
fn main() {
let a = <Mango as Parent>::Child {
a: 4
};
println!("{:?}", a);
}
编译这个有错误:
error: expected one of `.`, `::`, `;`, `?`, or an operator, found `{`
--> src/main.rs:25:38
|
25 | let a = <Mango as Parent>::Child {
| ^ expected one of `.`, `::`, `;`, `?`, or an operator here
我已经创建了一个宏来以类似的方式初始化一个结构,但我无法使用关联类型来完成它。我认为编译器由于某种原因不支持它。尽管如此,我还是想用这样的 API 创建一个宏。
我该如何解决这个问题?
【问题讨论】:
标签: rust