rust - 解决通过引用获取参数的闭包的类型不匹配

标签 rust lifetime ownership

我在尝试编译下面的 Rust 代码时遇到了一对奇怪的错误。在寻找其他有类似问题的人时,我遇到了 another question with the same combination of (seemingly opposing) errors ,但无法将解决方案从那里推广到我的问题。

基本上,我似乎遗漏了 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)
        } else {
            (q, max_d)
        }
    };

    fold(&points, (&Point { x: 0., y: 0. }, 0.), md)
}

我收到以下错误消息:

error[E0631]: type mismatch in closure arguments
  --> src/main.rs:23:5
   |
14 |     let md = |(q, max_d): (&Point, f32), p: &'a Point| -> (&Point, f32) {
   |              ---------------------------------------------------------- found signature of `for<'r> fn((&'r Point, f32), &'a Point) -> _`
...
23 |     fold(&points, (&Point { x: 0., y: 0. }, 0.), md)
   |     ^^^^ expected signature of `for<'r> fn((&Point, f32), &'r Point) -> _`
   |
   = note: required by `fold`

error[E0271]: type mismatch resolving `for<'r> <[closure@src/main.rs:14:14: 21:6] as std::ops::FnOnce<((&Point, f32), &'r Point)>>::Output == (&Point, f32)`
  --> src/main.rs:23:5
   |
23 |     fold(&points, (&Point { x: 0., y: 0. }, 0.), md)
   |     ^^^^ expected bound lifetime parameter, found concrete lifetime
   |
   = note: required by `fold`

(A Rust Playground link for this code, for convenience.)

在我看来,我提供给 fold 的函数应该正确地进行类型检查……我在这里遗漏了什么,我该如何修复它?

最佳答案

简短的版本是,如果闭包是内联编写的或存储为变量,则推断的生命周期之间存在差异。内联编写闭包并删除所有无关类型:

fn test(points: &[Point]) -> (&Point, f32) {
    let init = points.first().expect("No initial");
    fold(&points, (init, 0.), |(q, max_d), p| {
        let d = 12.;
        if d > max_d {
            (p, d)
        } else {
            (q, max_d)
        }
    })
}

如果您确实需要带外关闭,请查看 How to declare a lifetime for a closure argument? .

此外,我必须从输入数组中提取 first 值——您不能返回对局部变量的引用。该方法不需要生命周期参数;他们将被推断出来。

要真正让代码编译,您需要提供有关fold 方法的更多信息。具体来说,您必须指出传递给闭包的引用与传入的参数具有相同的生命周期。否则,它可能只是对局部变量的引用:

fn fold<'a, S, T, F>(item: &'a [S], accum: T, f: F) -> T
where
    F: Fn(T, &'a S) -> T,
{
    f(accum, &item[0])
}

相关的 Rust 问题是 #41078 .

关于rust - 解决通过引用获取参数的闭包的类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37910514/

相关文章:

rust - 包含一段数据和被引用数据的结构中的生命周期

recursion - 为什么我不需要显式借出一个借来的可变变量?

rust - 如何在不溢出的情况下对另一个数字进行算术模运算?

rust - 为什么不为 Box::from_raw() 调用析构函数?

loops - 具有生命周期界限的递归函数内循环中的借用检查器错误

rust - 拥有一个值的一般方式(不要指定 `Rc` 或 `Box` )

rust - 为什么 `a` 在 Rust 中保留 `let b = &*&a;` 之后的所有权?

rust - 如何在函数中借用 "take"切片的第一部分?

rust - 无法找到 [build-dependencies] 部分中列出的 crate

c++ - 隐式类型转换的临时工的生命周期