首页 > 解决方案 > 用于更大切片的 IntoIterator

问题描述

我正在尝试编写一个使用RegexSet. 这是使用它的代码。

fn main() {
    let path_regex = regex::RegexSet = regex::RegexSet::new(&[
        r"^/api/$",
        ...
        r"^/logs/$",
        r"^/logs/(?P<logpath>[^/?#]*)$",
        r"^/version/$"
    ]).unwrap();
}

传递给的切片RegexSet有 346 个元素。当我运行这个时,我得到

error[E0277]: `&[&str; 346]` is not an iterator
   --> src/server/mod.rs:650:60
    |
650 |         pub static ref GLOBAL_REGEX_SET: regex::RegexSet = regex::RegexSet::new(&[
    |                                                            ^^^^^^^^^^^^^^^^^^^^ `&[&str; 346]` is not an iterator
    |
    = help: the trait `std::iter::Iterator` is not implemented for `&[&str; 346]`
    = note: required because of the requirements on the impl of `std::iter::IntoIterator` for `&[&str; 346]`
    = note: required by `server::paths::regex::RegexSet::new`

查看文档,IntoIterator针对大小不超过 32 的数组实现

我试图实现该特征,导致错误

error[E0119]: conflicting implementations of trait `std::iter::IntoIterator` for type `&[_; 346]`:
   --> src/server/mod.rs:651:5
    |
651 |     impl<'a, T> IntoIterator for &'a [T; 346] {
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: conflicting implementation in crate `core`:
            - impl<I> std::iter::IntoIterator for I
              where I: std::iter::Iterator;
    = note: upstream crates may add new impl of trait `std::iter::Iterator` for type `&[_; 346]` in future versions

我该如何解决这个问题?

标签: rust

解决方案


推荐阅读