简体   繁体   English

如何在另一个泛型类型的特征绑定的类型参数上表达特征?

[英]How can I express a trait bound on a type parameter for another generic type's trait bound?

I'm trying to improve some existing code I to make it more generic, by adding a type variable in place of a concrete type. 我正在尝试通过添加一个类型变量来代替具体类型来改进现有的一些代码,使其更通用。

The original code looked like this: 原始代码如下所示:

fn parse_row(text: String) -> Result<Vec<u32>, String> {
    text.split(" ")
        .map(|s| s.to_owned()
            .parse::<u32>()
            .map_err(|e| e.to_string())
        )
        .collect()
}

And here is the generic version: 这是通用版本:

fn parse_row<T>(text: String) -> Result<Vec<T>, String>
where
    T: Copy + Debug + FromStr + Display,
{
    text.split(" ")
        .map(|s| s.to_owned()
            .parse::<T>()
            .map_err(|e| e.to_string())
        )
        .collect()
}

The error I get is: 我得到的错误是:

error[E0599]: no method named `to_string` found for type `<T as std::str::FromStr>::Err` in the current scope
 --> src/main.rs:7:28
  |
7 |             .map_err(|e| e.to_string())
  |                            ^^^^^^^^^
  |
  = note: the method `to_string` exists but the following trait bounds were not satisfied:
          `<T as std::str::FromStr>::Err : std::string::ToString`

<T as core::str::FromStr>::Err is referring to the type parameter associated with T 's FromStr implementation, but how can I express that this type — that I can't actually know — has the Display trait? <T as core::str::FromStr>::Err指的是与TFromStr实现相关的类型参数,但是我如何表达这种类型 - 我实际上无法知道 - 具有Display特征?

This was confusing initially because I didn't understand which Err it was referring to - and thought it was the error type parameter for Result . 这最初令人困惑,因为我不明白它指的是哪个Err - 并且认为它是Result的错误类型参数。 Once I figured out that FromStr has its own Err type parameter, I just had to work out how to express that constraint. 一旦我发现FromStr有自己的Err类型参数,我只需要弄清楚如何表达这个约束。 And here it is: 这是:

fn parse_row<T>(text: String) -> Result<Vec<T>, String>
where
    T: Copy + Debug + FromStr,
    T::Err: Display,
{
    text.split(" ")
        .map(|s| s.to_owned()
            .parse::<T>()
            .map_err(|e| e.to_string())
        )
        .collect()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果在函数内创建引用,如何将泛型类型与需要使用生命周期参数的特征绑定在一起? - How do I bound a generic type with a trait that requires a lifetime parameter if I create the reference inside the function? 性状受制于普通特征 - Trait bound on generic trait 如何写一个特征绑定添加两个泛型类型的引用? - How to write a trait bound for adding two references of a generic type? 如何将特征绑定到非泛型类型? - How to add trait bound to a non-generic type? 如何在特征本身上写一个特征绑定来引用关联类型? - How to write a trait bound for a reference to an associated type on the trait itself? 为具有嵌套边界的类型实现特征 - Implement trait for type with nested bound 函数如何要求类型实现特征而不删除现有特征界限? - How can a function require that a type implement a trait without removing the existing trait bound? 依赖于可选特征界限的默认泛型参数 - Default generic parameter that depends on an optional trait bound 如何说明一个特征是否等同于给定类型参数的另一个特征? - How to state one trait is the equivalent of another generic trait for a given type parameter? 我应该如何使用一个受我的特征绑定的通用参数为我的结构实现 From/Into? - How should I implement From/Into for my struct with one generic parameter bound by my trait?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM