【发布时间】:2017-12-05 17:04:27
【问题描述】:
我正在尝试实现 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 的完整代码。
【问题讨论】:
-
错误信息非常令人困惑,并且与 AFAICT 无关;可能想要提交一个错误报告。真正的问题是
str::contains()想要一个具有thePatterntrait 的类型,但&Path没有实现该特征。&str确实如此,所以 this 有效,但我不知道这是否真的是你想要的(因此发表评论而不是回答)...... -
嘿,这确实解决了我的问题。 :D 一个疑问,这里为什么是查询路径?
-
因为
run有where T: std::convert::AsRef<std::path::Path>