文档Split说它实现了DoubleEndedIterator应该有一个next_back()获取最后一个元素的方法。
但是当我这样做时:
fn get_file_ext(file_name: &str) -> Option<String> {
if let Some(ext) = file_name.split(".").next_back() {
return Some(ext.to_owned());
}
None
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
error[E0599]: no method named `next_back` found for struct `std::str::Split<'_, &str>` in the current scope
--> src/lib.rs:2:45
|
2 | if let Some(ext) = file_name.split(".").next_back() {
| ^^^^^^^^^ method not found in `std::str::Split<'_, &str>`
|
= note: the method `next_back` exists but the following trait bounds were not satisfied:
`std::str::pattern::StrSearcher<'_, '_>: std::str::pattern::DoubleEndedSearcher<'_>`
which is required by `std::str::Split<'_, &str>: std::iter::DoubleEndedIterator`
Run Code Online (Sandbox Code Playgroud)
这是什么意思"the following trait bounds were not satisfied"?
代替
file_name.split(".")
Run Code Online (Sandbox Code Playgroud)
和
file_name.split('.')
Run Code Online (Sandbox Code Playgroud)
这是 trait 实现声明:
impl<'a, P> DoubleEndedIterator for Split<'a, P>
where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: DoubleEndedSearcher<'a>,
Run Code Online (Sandbox Code Playgroud)
这里缺少的特征绑定是
<P as Pattern<'a>>::Searcher: DoubleEndedSearcher<'a>,
Run Code Online (Sandbox Code Playgroud)
此 trait bound 是为 的搜索者实现的,char但不是为 实现的&str。
char::Searcher是 aDoubleEndedSearcher因为搜索 achar只需要一次查看一个,这在两端的行为相同。
(&str)::Searcher不是 aDoubleEndedSearcher因为"aa"haystack"aaa"中的模式匹配为"[aa]a"或"a[aa]",取决于从哪一侧搜索。
否则说:并非所有模式都允许双端搜索。Achar允许它但不是字符串。
| 归档时间: |
|
| 查看次数: |
183 次 |
| 最近记录: |