获取错误“特征 `std::ops::FnMut<(char,)>` 未为 `std::string::String` 实现";对于简单的类型不匹配 [英] Getting error "the trait `std::ops::FnMut<(char,)>` is not implemented for `std::string::String`" for a simple type mismatch
问题描述
let mystring = format!("the quick brown {}", "fox...");断言!(mystring.ends_with(mystring));
错误:
特性 `std::ops::FnMut<(char,)>` 没有为 `std::string::String` 实现
将 mystring.ends_with(mystring)
更改为 mystring.ends_with(mystring.as_str())
修复了它.
为什么这个错误如此神秘?
如果我在不使用格式的情况下创建字符串,请说:
let mystring = String::from_str("The quick brown fox...");断言!(mystring.ends_with(mystring));
错误更容易理解:
error[E0599]: 没有找到类型名为 `ends_with` 的方法`std::result::Result<std::string::String, std::string::ParseError>`在当前范围内
这个错误还有很多:
<代码>|断言!(mystring.ends_with(mystring));|^^^^^^^^^ `std::ops::FnMut<(char,)>` 没有为 `std::string::String` 实现|= 注意:因为对`std::string::String` 的`std::str::pattern::Pattern<'_>` impl 的要求是必需的
批判性
<块引用>注意:因为std::string::String
的std::str::pattern::Pattern<'_>
的impl的要求,所以需要
String
的.ends_with
接受任何实现了 Pattern
特征的值作为它的搜索模式,而 String
没有实现那个特征.>
如果您查看 Pattern<的文档/code>
,它包括
impl<'a, 'b>模式<'a>for &'b 字符串
所以如果您更改,您的代码段就可以正常工作
assert!(mystring.ends_with(mystring));
到
assert!(mystring.ends_with(&mystring));
这也是有道理的,因为否则你会试图将 mystring
的所有权传递给 ends_with
函数,这似乎不是没错.
至于你看到的具体错误,Pattern
的 trait 定义也包括
impl<'a, F>模式<'a>对于 F在哪里F: FnMut(char) ->布尔,
通常表示函数接受一个字符并返回一个布尔计数作为模式,导致消息说 String
与该特征实现不匹配.
let mystring = format!("the quick brown {}", "fox...");
assert!(mystring.ends_with(mystring));
Error:
the trait `std::ops::FnMut<(char,)>` is not implemented for `std::string::String`
Changing mystring.ends_with(mystring)
to mystring.ends_with(mystring.as_str())
fixes it.
Why is this error so cryptic?
If I create the string without using format, say:
let mystring = String::from_str("The quick brown fox...");
assert!(mystring.ends_with(mystring));
The error is much more understandable:
error[E0599]: no method named `ends_with` found for type
`std::result::Result<std::string::String, std::string::ParseError>`
in the current scope
There's more to that error:
| 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`
critically
note: required because of the requirements on the impl of
std::str::pattern::Pattern<'_>
forstd::string::String
String
's .ends_with
accepts any value that implements the Pattern
trait as its search pattern, and String
does not implement that trait.
If you look at the documentation for Pattern
, it includes
impl<'a, 'b> Pattern<'a> for &'b String
so your snippet works fine if you change
assert!(mystring.ends_with(mystring));
to
assert!(mystring.ends_with(&mystring));
It also makes sense because otherwise you'd be trying to pass ownership of mystring
to the ends_with
function, which doesn't seem right.
As for the specific error you were seeing, Pattern
's trait definition also includes
impl<'a, F> Pattern<'a> for F
where
F: FnMut(char) -> bool,
which generically says functions accepting a char and returning a boolean count as patterns, leading to the message saying String
does not match that trait implementation.
这篇关于获取错误“特征 `std::ops::FnMut<(char,)>` 未为 `std::string::String` 实现";对于简单的类型不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!