gpt4 book ai didi

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

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

我正在尝试实现 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
}

我收到这个错误:

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`

为什么我需要 Fn 特性?添加该特征并不能解决我的问题。我正在使用泛型,我知道我在这里并不真的需要泛型,但我正在尝试理解这个主题。

这里是 Rust 的完整代码 playground .

最佳答案

问题(或其中之一)出在这个函数中:

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
}

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

您将面临的下一个问题是 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
}

此后您仍然会遇到更多错误,但希望这能帮助您克服最令人困惑的错误。

关于generics - 特性 `std::ops::Fn<(char,)>` 没有为 `T` 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44868443/

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