还原str.split的结果

问题描述 投票:4回答:3

根据https://doc.rust-lang.org/std/str/struct.Split.html,对字符串执行rev的结果有split方法

我收到以下错误

error: the trait `core::str::pattern::DoubleEndedSearcher<'_>` is not
implemented for the type `core::str::pattern::StrSearcher<'_, '_>` [E0277]
for part in "1:30".split(":").rev() {

我使用的代码

let mut length = 0;
let mut mult = 1;
for part in "1:30".split(":").rev() {
   length += mult * part.parse::<i32>().unwrap();
   mult *= 60;
}
string rust string-parsing
3个回答
7
投票

[问题是,仅当rev()迭代器实现Split时才在DoubleEndedIterator迭代器上定义,但是Split仅当您要分割的模式的搜索者满足DoubleEndedIterator时才实现DoubleEndedSearcher:] >

impl<'a, P> DoubleEndedIterator for Split<'a, P> where P: Pattern<'a>, P::Searcher: DoubleEndedSearcher<'a>

6
投票

其他答案正确,但是我想指出rsplit。这可能更明显,更有效。

所以,为什么不能使用rsplit?其他答案指出,rev尚未实现。但是为什么


2
投票

TLDR

"baaab".split("aa").rev() // -> ["b", "aa", "ba"] (用于搜索字符串模式的类型)没有实现rsplit,因此,StrSearcher迭代器没有实现DoubleEndedSearcher,因此,您不能在其上调用split
© www.soinside.com 2019 - 2024. All rights reserved.