According to the docs for Split
, there is a rev
method on the result of doing split
on a string:
fn main() {
let mut length = 0;
let mut mult = 1;
for part in "1:30".split(":").rev() {
length += mult * part.parse::<i32>().unwrap();
mult *= 60;
}
}
I get the following error:
error[E0277]: the trait bound `std::str::pattern::StrSearcher<'_, '_>: std::str::pattern::DoubleEndedSearcher<'_>` is not satisfied
--> src/main.rs:4:35
|
4 | for part in "1:30".split(":").rev() {
| ^^^ the trait `std::str::pattern::DoubleEndedSearcher<'_>` is not implemented for `std::str::pattern::StrSearcher<'_, '_>`
|
= note: required because of the requirements on the impl of `std::iter::DoubleEndedIterator` for `std::str::Split<'_, &str>`
error[E0277]: the trait bound `std::str::pattern::StrSearcher<'_, '_>: std::str::pattern::DoubleEndedSearcher<'_>` is not satisfied
--> src/main.rs:4:17
|
4 | for part in "1:30".split(":").rev() {
| ^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::str::pattern::DoubleEndedSearcher<'_>` is not implemented for `std::str::pattern::StrSearcher<'_, '_>`
|
= note: required because of the requirements on the impl of `std::iter::DoubleEndedIterator` for `std::str::Split<'_, &str>`
= note: required because of the requirements on the impl of `std::iter::Iterator` for `std::iter::Rev<std::str::Split<'_, &str>>`
Example 2: Reverse a String Using built-in Methods First, the string is split into individual array elements using the split() method. str. split("") gives ["h", "e", "l", "l", "o"] . The string elements are reversed using the reverse() method.
We can reverse each word of a string by the help of reverse(), split() and substring() methods. By using reverse() method of StringBuilder class, we can reverse given string. By the help of split("\\s") method, we can get all words in an array. To get the first character, we can use substring() or charAt() method.
The other fundamental string operation is the opposite of splitting strings: string concatenation.
The other answers are correct, but I want to point out rsplit
. This is probably more obvious and more performant.
So, why can't you use rev
? As other answers state, it's not implemented for StrSearcher
. But why is it not implemented? From the DoubleEndedSearcher
docs:
For this, the impl of
Searcher
andReverseSearcher
need to follow these conditions:
- All results of
next()
need to be identical to the results ofnext_back()
in reverse order.next()
andnext_back()
need to behave as the two ends of a range of values, that is they can not "walk past each other".
The problem with reversing the iterator using strings is this:
"baaab".split("aa") // -> ["b", "aa", "ab"];
However, if you were to start at the end of the string, you'd get something like:
"baaab".split("aa").rev() // -> ["b", "aa", "ba"]
Which is clearly not the same set of items in a different order!
Simply put, you can't reverse an iterator that is split on strings because there's no efficient way of knowing when the next result is. You'd have to split the entire string into a collection, then reverse the collection!
This is why rsplit
exists - it means start at the end of the string and split to the beginning, in an efficient manner.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With