gpt4 book ai didi

rust - 不匹配的类型 : Expected struct, 找到关联类型

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

<分区>

我定义了一些 TryFrom 实例,并想编写一个可以使用所有这些类型的函数。

use std::convert::TryFrom;

// several structs like this that are TryFrom<i32>
struct SuperiorThanZero(i32);

impl TryFrom<i32> for SuperiorThanZero {
type Error = String;

fn try_from(value: i32) -> Result<Self, Self::Error> {
unimplemented!()
}
}

fn from_three<T>() -> Result<T, String>
where
T: TryFrom<i32>,
{
T::try_from(3)
}

这不编译:

error[E0308]: mismatched types
--> src/lib.rs:16:9
|
12 | fn from_three<T>() -> Result<T, String>
| ----------------- expected `std::result::Result<T, std::string::String>` because of return type
...
16 | T::try_from(3)
| ^^^^^^^^^^^^^^ expected struct `std::string::String`, found associated type
|
= note: expected type `std::result::Result<_, std::string::String>`
found type `std::result::Result<_, <T as std::convert::TryFrom<i32>>::Error>`

如果我将错误类型定义为 &'static str,消息将更改为 Expected reference, found associated type

有多个关于错误 Expected associated type, found struct [...] 的问题,即与 associated type 和交换的具体类型,但他们没有看起来不相关。

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