【发布时间】:2020-03-09 01:22:14
【问题描述】:
我正在尝试使用“地图”概念来实现特征。我想出了以下最小示例:
trait Value<T> {
fn get(&self) -> T;
}
struct ValueMap<S, F> {
s: S,
f: F,
}
impl<T, U, S: Value<T>, F: Fn(T) -> U> Value<U> for ValueMap<S, F> {
fn get(&self) -> U {
(self.f)(self.s.get())
}
}
我收到错误the type parameterTis not constrained by the impl trait, self type, or predicates。
当F 是将值S 映射到其他值的函数时,我如何为我的ValueMap 结构实现Value 特征?
备注:我在 Value 上使用关联类型时没有这个问题。但是这些概念对我来说还是有点模糊。
【问题讨论】: