我在尝试编译下面的 Rust 代码时遇到了一对奇怪的错误。在寻找具有类似问题的其他人时,我遇到了另一个问题,该问题具有相同的(看似相反的)错误组合,但无法将解决方案从那里推广到我的问题。
基本上,我似乎忽略了 Rust 所有权系统的一个微妙之处。尝试在这里编译(非常精简的)代码:
struct Point {
x: f32,
y: f32,
}
fn fold<S, T, F>(item: &[S], accum: T, f: F) -> T
where
F: Fn(T, &S) -> T,
{
f(accum, &item[0])
}
fn test<'a>(points: &'a [Point]) -> (&'a Point, f32) {
let md = |(q, max_d): (&Point, f32), p: &'a Point| -> (&Point, f32) {
let d = p.x + p.y; // Standing in for a function call
if d > max_d {
(p, d) …Run Code Online (Sandbox Code Playgroud)