为什么我不能反转 str::split 的结果? [英] Why can I not reverse the result of str::split?

查看:31
本文介绍了为什么我不能反转 str::split 的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 Split 的文档,对一个字符串做split的结果有一个rev方法:

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;
    }
}

我收到以下错误:

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>>`

推荐答案

问题是 rev() 定义在 Split 迭代器上,只有当它实现了 DoubleEndedIterator,但 Split 仅在您拆分的模式的搜索器满足 DoubleEndedSearcher 时才实现 DoubleEndedIterator:

The problem is that rev() is defined on the Split iterator only if it implements DoubleEndedIterator, but Split only implements DoubleEndedIterator if the searcher of the pattern you are splitting on satisfies DoubleEndedSearcher:

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

文档列出了哪些类型实现了 DoubleEndedSearcher.那里的类​​型都没有对应于 &str 模式,所以当你拆分字符串时不能使用 rev().

The documentation lists which types implement DoubleEndedSearcher. None of the types there correspond to &str pattern, so you can't use rev() when you split on string.

在您的特定情况下,我想,将 split(":") 更改为 split(':') 就足够了(即在字符上拆分而不是字符串),因为字符模式搜索器确实实现了DoubleEndedSearcher.

In your particular case, I guess, it is sufficient to change split(":") to split(':') (i.e. split on character instead of string) because the character pattern searcher does implement DoubleEndedSearcher.

Rust 的这些特性(条件 trait 实现和方法本地的 trait bound)允许编写真正具有表现力的代码,但它们有时难以通读.

Such features of Rust (conditional trait implementation and trait bounds local to a method) allow writing really expressive code, but they can be sometimes hard to read through.

这篇关于为什么我不能反转 str::split 的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆