该错误还有更多内容:
| assert!(mystring.ends_with(mystring));
| ^^^^^^^^^ the trait `std::ops::FnMut<(char,)>` is not implemented for `std::string::String`
|
= note: required because of the requirements on the impl of `std::str::pattern::Pattern<'_>` for `std::string::String`
批判地
注意:必需的,因为对 impl 的要求std::str::pattern::Pattern<'_>
for std::string::String
String's .ends_with https://doc.rust-lang.org/std/string/struct.String.html#method.ends_with接受任何实现的值Pattern
特征作为其搜索模式,以及String
没有实现该特征。
如果你看的文档Pattern https://doc.rust-lang.org/std/str/pattern/trait.Pattern.html, 这包括
impl<'a, 'b> Pattern<'a> for &'b String
所以如果你改变你的代码片段就可以正常工作
assert!(mystring.ends_with(mystring));
to
assert!(mystring.ends_with(&mystring));
这也是有道理的,因为否则你会试图通过所有权 of mystring
to the ends_with
函数,这似乎不对。
至于您看到的具体错误,Pattern
的特质定义还包括
impl<'a, F> Pattern<'a> for F
where
F: FnMut(char) -> bool,
一般说函数接受字符并返回布尔计数作为模式,导致消息说String
与该特征实现不匹配。