【发布时间】:2017-10-05 09:21:41
【问题描述】:
impl Rate for Vec<VolumeRanged> {
fn costs<'a, I>(&'a self, issues: &I, period: u32) -> Box<'a + Iterator<Item = f32>>
where
I: IntoIterator<Item=f32>,
I::IntoIter: 'a
{
fn issue_cost(issue: f32, percentage: f32, constant: f32, max: f32) -> f32 {
let r = issue * percentage / 100.0 + constant;
match r.partial_cmp(&max) {
Some(Less) => r,
_ => max
}
}
Box::new(issues.into_iter().map(|i| {
let (pr, nr) = pairwise(self)
.find(|&(_, n)| in_range(&i, &n.range))
.expect("No range found");
issue_cost(
i,
nr.percentage,
pr.map_or(0.0, |x| x.max),
nr.max,
)
}))
}
}
锈在说
error[E0373]: closure may outlive the current function, but it borrows `self`, which is owned by the current function
--> src/main.rs:43:41
|
43 | Box::new(issues.into_iter().map(|i| {
| ^^^ may outlive borrowed value `self`
44 | let (pr, nr) = pairwise(self)
| ---- `self` is borrowed here
|
help: to force the closure to take ownership of `self` (and any other referenced variables), use the `move` keyword
|
43 | Box::new(issues.into_iter().map(move |i| {
| ^
但我不想关闭所有权。我想要的是返回的盒装迭代器应该与self 和issues 一样长。他们一走,它就应该走。
我知道这可以通过将克隆的迭代器传递给 issues 而不是对它的引用来解决,我认为这里不需要它。
【问题讨论】: