0

I am trying to write an API server that uses RegexSet. Here is the code that uses it.

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

The slice passed to RegexSet has 346 elements. When I run this, I get

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`

Looking at the documentation, IntoIterator has only been implemented for arrays of size up to 32

I tried to implement that trait, resulting in the error

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

How do I work around this?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
ACC
  • 2,488
  • 6
  • 35
  • 61
  • 2
    Due to the current limitations of array types, only arrays up to length 32 implement a good portion of traits in the standard library, including `IntoIterator`. You can turn that array into a slice (`[...].as_ref()`), which does implement `IntoIterator`. – E_net4 Feb 10 '19 at 23:50
  • 3
    Or via `&[/* elements */][..]`. – Shepmaster Feb 11 '19 at 00:06

0 Answers0