{ value: T, func: F } fn main() { let lambda = |&x, -6ren">
gpt4 book ai didi

Rust "expected type"错误打印完全相同的不匹配类型

转载 作者:行者123 更新时间:2023-11-29 08:08:28 25 4
gpt4 key购买 nike

夜间使用rust :

Playground

struct Foo<T, F: Fn(&T, &T) -> T> {
value: T,
func: F
}

fn main() {
let lambda = |&x, &y| x + y;
let foo = Foo {
value: 5 as i32,
func: lambda
};
}

错误信息:

Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
--> src/main.rs:8:15
|
8 | let foo = Foo {
| ^^^ one type is more general than the other
|
= note: expected type `std::ops::FnOnce<(&i32, &i32)>`
found type `std::ops::FnOnce<(&i32, &i32)>`

请注意,预期类型和找到的类型是完全相同的字符。为什么错误消息说一种类型比另一种更通用,同时还说它们是同一类型?

最佳答案

With nightly rust:

这似乎只是夜间构建中的“错误”错误消息。在 Rust 1.32(稳定版)中,错误告诉您这是生命周期不匹配:

error[E0631]: type mismatch in closure arguments
--> src/main.rs:8:15
|
7 | let lambda = |&x, &y| x + y;
| -------------- found signature of `fn(&_, &_) -> _`
8 | let foo = Foo {
| ^^^ expected signature of `for<'r, 's> fn(&'r i32, &'s i32) -> _`
|
note: required by `Foo`
--> src/main.rs:1:1
|
1 | struct Foo<T, F: Fn(&T, &T) -> T> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0271]: type mismatch resolving `for<'r, 's> <[closure@src/main.rs:7:18: 7:32] as std::ops::FnOnce<(&'r i32, &'s i32)>>::Output == i32`
--> src/main.rs:8:15
|
8 | let foo = Foo {
| ^^^ expected bound lifetime parameter, found concrete lifetime
|
note: required by `Foo`
--> src/main.rs:1:1
|
1 | struct Foo<T, F: Fn(&T, &T) -> T> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Why is the error message saying that one type is more general than the other, while also saying that they are the same type?

类型仅在生命周期上有所不同。夜间消息不包括生命周期——也许是为了在生命周期不相关的情况下减少噪音。显然,当生命周期是类型之间的唯一区别时,这根本没有帮助。

考虑 reporting a bug给 Rust 团队。

关于Rust "expected type"错误打印完全相同的不匹配类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54341465/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com