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,
)
}))
}
}
Rust正在说
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
而不是对它的引用来解决,我不认为这里需要它。
答案 0 :(得分:1)
将move
添加到闭包中。 self
的类型为&Vec<VolumeRanged>
,因此闭包不会取得向量的所有权,它会捕获对向量的引用。