Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't a range of char be collected?

I'm trying to generate a vector containing lowercase ASCII characters. This more convoluted approach works:

let ascii_lowercase = (b'a'..=b'z').map(|b| b as char).collect::<Vec<char>>();

But this more straightforward one, which I came up with in the first place, does not:

let ascii_lowercase = ('a'..='z').collect::<Vec<char>>();

The error is:

error[E0599]: no method named `collect` found for type `std::ops::RangeInclusive<char>` in the current scope
 --> src/main.rs:2:39
  |
2 |     let ascii_lowercase = ('a'..='z').collect::<Vec<char>>();
  |                                       ^^^^^^^
  |
  = note: the method `collect` exists but the following trait bounds were not satisfied:
          `std::ops::RangeInclusive<char> : std::iter::Iterator`
          `&mut std::ops::RangeInclusive<char> : std::iter::Iterator`

Which is weird, because as far as I understand, there is a blanket implementation of Iterator for RangeInclusive.

Is it impossible to use a range of chars as an iterator? If so, why? If not, what am I doing wrong? I'm using stable Rust 2018 1.31.1.

like image 995
dlukes Avatar asked Dec 29 '18 17:12

dlukes


2 Answers

EDIT 2020-07-17: since Rust 1.45.0, the trait Step is implemented for char, making Range<char> (and some other char ranges) work as an iterator. The code in the question now compiles without problem!

Old answer below.


The expression b'a'..=b'z' has the type RangeInclusive<u8> (see on Playground) because the expression b'a' has the type u8: that's what the b in front of the character literal is for. On the other hand, the expression 'a'..='z' (without the bs) has the type RangeInclusive<char>.

[...] there is a blanket implementation of Iterator for RangeInclusive.

For one, this is not what we call "blanket implementation" (this is when the impl block is for T or for &T (or similar) with T being a generic type). But yes, there is an impl. But let's take a closer look:

impl<A> Iterator for RangeInclusive<A> 
where
    A: Step,   // <--- important

The A: Step bound is important. As you can see in the documentation for Step, this trait is implemented for all primitive integer types, but not for char. This means that there is no clear "add one" operation on characters. Yes, you could define it to be the next valid Unicode codepoint, but the Rust developers probably decided against that for a good reason.

As a consequence, RangeInclusive<char> does not implement Iterator.

So your solution is already a good one. I would probably write this:

(b'a'..=b'z').map(char::from).collect::<Vec<_>>()

The only real advantage is that in this version, char doesn't appear twice.

like image 134
Lukas Kalbertodt Avatar answered Oct 03 '22 13:10

Lukas Kalbertodt


The problem was that the iteration capabilities of range types depend on the Step trait (see extended answer). However, starting from Rust 1.45, char also implements Step (PR 72413), which means that the code in the question now works!

let ascii_lowercase: Vec<char> = ('a'..='h').collect();
assert_eq!(
    ascii_lowercase,
    vec!['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
);
like image 43
E_net4 stands with Ukraine Avatar answered Oct 03 '22 14:10

E_net4 stands with Ukraine