特性`std :: ops :: Fn <(char,)>`没有为`T`实现

cod*_*oob 2 generics traits rust

我正在尝试实现greps项目,我陷入了搜索功能.

fn search<'a, T>(query: &T, contents: &'a str) -> Vec<&'a str> {
    let mut results = Vec::new();

    for line in contents.lines() {
        if line.contains(query) {
            results.push(line);
        }
    }
    results
}
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

rustc 1.18.0 (03fc9d622 2017-06-06)
error[E0277]: the trait bound `T: std::ops::Fn<(char,)>` is not satisfied
  --> <anon>:39:17
   |
39 |         if line.contains(query) {
   |                 ^^^^^^^^ the trait `std::ops::Fn<(char,)>` is not implemented for `T`
   |
   = help: consider adding a `where T: std::ops::Fn<(char,)>` bound
   = note: required because of the requirements on the impl of `std::ops::FnOnce<(char,)>` for `&T`
   = note: required because of the requirements on the impl of `std::str::pattern::Pattern<'_>` for `&T`

error[E0277]: the trait bound `T: std::ops::FnOnce<(char,)>` is not satisfied
  --> <anon>:39:17
   |
39 |         if line.contains(query) {
   |                 ^^^^^^^^ the trait `std::ops::FnOnce<(char,)>` is not implemented for `T`
   |
   = help: consider adding a `where T: std::ops::FnOnce<(char,)>` bound
   = note: required because of the requirements on the impl of `std::str::pattern::Pattern<'_>` for `&T`

error[E0277]: the trait bound `str: std::marker::Sized` is not satisfied
  --> <anon>:57:40
   |
57 |         assert_eq!(vec!["KAMEHAMEHA"], search(query, contents));
   |                                        ^^^^^^ the trait `std::marker::Sized` is not implemented for `str`
   |
   = note: `str` does not have a constant size known at compile-time
   = note: required by `search`
Run Code Online (Sandbox Code Playgroud)

为什么我需要Fn特质?添加该特性并不能解决我的问题.我正在使用泛型,我知道我真的不需要泛型,但我想了解这个主题.

这是Rust 操场上的完整代码.

Pet*_*all 5

问题(或其中之一)在此功能中:

fn search<'a, T>(query: &T, contents: &'a str) -> Vec<&'a str> {
    let mut results = Vec::new();

    for line in contents.lines() {
        if line.contains(query) {
            results.push(line);
        }
    }
    results
}
Run Code Online (Sandbox Code Playgroud)

该函数line.contains()需要一个实现特征的参数Pattern.但是你的T参数是不受约束的.这个错误有点令人困惑,因为你可能不关心FnFnOnce实现,这恰好是超类型Pattern.

您将面临的下一个问题是它Pattern本身尚未稳定,因此除非您切换到编译器的每晚构建,否则无法显式使用它.否则,您可以约束,T以便至少可以将其转换为已实现的其他内容Pattern,例如&str:

fn search<'a, T: 'a>(query: &'a T, contents: &'a str) -> Vec<&'a str> 
    where &'a T: Into<&'a str>
{
    let mut results = Vec::new();

    for line in contents.lines() {
        if line.contains(query.into()) {
            results.push(line);
        }
    }
    results
}
Run Code Online (Sandbox Code Playgroud)

在此之后你仍然会有更多的错误,但希望这会让你经历最令人困惑的错误.