【发布时间】:2019-01-19 18:08:38
【问题描述】:
我能够使这段代码工作:
fn twice<T: Clone>(fst: impl Fn(T), snd: impl Fn(T)) -> impl Fn(T) {
move |t| {
fst(t.clone());
snd(t)
}
}
但是,我想要的是这个(没有拳击):
fn sub<T: Clone>(mut fst: impl Fn(T), snd: impl Fn(T)) {
fst = move |t: T| {
fst(t.clone());
snd(t)
};
}
有没有一种方法可以让第二段代码在没有装箱、使用特征、类型转换或任何其他方法的情况下工作? Rust 抱怨类型不匹配。
【问题讨论】:
-
起初您的方法是错误的,您将
fst移动到sub函数中,然后尝试更改它的值。但什么都不返回?fst将被放入子函数中。