为什么生锈失败的类型推理vec< _>

发布于 2025-02-11 06:04:04 字数 705 浏览 3 评论 0 原文

我不明白为什么Rust无法推断 vec< _> 的类型。如果我运行此问题,

fn main() {
    let v = vec![1, 2, 3];
    let s = v.iter().sum();
}

我会收到一个错误:

error[E0282]: type annotations needed
 --> src/main.rs:5:9
  |
5 |     let s = v.iter().sum();
  |         ^ consider giving `s` a type

因此我必须将其修改为任何一种

fn main() {
    let v = vec![1, 2, 3];
    let s = v.iter().sum::<i32>();
}

类型

fn main() {
    let v = vec![1, 2, 3];
    let s: i32 = v.iter().sum();
}

推理在此处失败的原因?它正确地得到 v vec&lt; i32&gt; ,因此,如果 sum 在该向量上,为什么需要 i32 被注释?

I don't understand why Rust fails to infer the type for Vec<_> in some cases. If I run this

fn main() {
    let v = vec![1, 2, 3];
    let s = v.iter().sum();
}

I get an error:

error[E0282]: type annotations needed
 --> src/main.rs:5:9
  |
5 |     let s = v.iter().sum();
  |         ^ consider giving `s` a type

so that I have to modify it to either

fn main() {
    let v = vec![1, 2, 3];
    let s = v.iter().sum::<i32>();
}

or

fn main() {
    let v = vec![1, 2, 3];
    let s: i32 = v.iter().sum();
}

Why the type inference fails here? It correctly gets that v is a Vec<i32> so if sum it's on that vector why does it need i32 to be annotated?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪荡不羁 2025-02-18 06:04:04

要扩展Chayim Friedman的答案,请考虑以下代码:

use core::iter::Sum;

// here might be some complex state, e.g. a kind of "long arithmetic"
struct Consumer;

impl<'a> Sum<&'a i32> for Consumer {
    fn sum<I: Iterator<Item = &'a i32>>(_: I) -> Self {
        // here might be some complex logic
        Self
    }
}

fn main() {
    let v = vec![1, 2, 3];
    let s: Consumer = v.iter().sum();
}

游乐场

您可以看到 main 函数与示例仅在 s 的类型中有所不同。因此,如果没有这种类型,它可能是模棱两可的 - 请注意,消费者 struct> struct> struct> struct(以及其实现)很可能在另一个板条箱中,而不是由您控制。因此,生锈迫使您做出明确的选择,确切地汇总到什么。

To expand on the Chayim Friedman's answer, consider the following code:

use core::iter::Sum;

// here might be some complex state, e.g. a kind of "long arithmetic"
struct Consumer;

impl<'a> Sum<&'a i32> for Consumer {
    fn sum<I: Iterator<Item = &'a i32>>(_: I) -> Self {
        // here might be some complex logic
        Self
    }
}

fn main() {
    let v = vec![1, 2, 3];
    let s: Consumer = v.iter().sum();
}

Playground

You can see that the main function differs from your example only in the type of s. Therefore, without this type it's potentially ambiguous - note that the Consumer struct (and, consequently, its implementation) could very well be in another crate, not controlled by you. Therefore, Rust forces you to make an explicit choice, what exactly to sum into.

盗梦空间 2025-02-18 06:04:04

iterator> iterator :: sum(sum() /code> 对其返回类型是通用的。这使您可以将&amp; i32 s的迭代器汇总到 i32 中,因为 i32 i32 impl sum&sum&lt&lt&lt; 。因此,编译器无法推断返回类型。

Iterator::sum() is generic over its return type. This allows you to e.g. sum an iterator of &i32s into an i32, since i32 impls Sum<&i32>. Because of that, the compiler cannot infer the return type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文