Expand description
Adds vector, set, map, and iterator comprehensions to Rust. This is achieved through various functional macros, which are:
vec_comp!: returns aVecset_comp!: returns aHashSetmap_comp!: returns aHashMapiter_comp!: returns an iterator (this is used internally byvec_compandset_comp)
How to use
For a full explanation of how to use the macros, see the documentation for
vec_comp!. map_comp! is a bit different, but it has examples as well.
What about mapcomp?
I’m aware of the existence of the mapcomp
crate, although it differs from this crate in a few ways. For starters,
mapcomp aims to make their syntax as close to Python as possible and
I think they did a great job; this crate is not trying to do that. The
goal of this crate is to add comprehensions to Rust in an idiomatic way
so that the syntax flows naturally with the rest of the language while
still being concise and powerful.
On a more technical note, mapcomp uses generators internally which was
okay for Rust 2018, but generators and yield-ing are now experimental
features. This was a big inspiration for this crate, as I wanted to make
a macro-based solution that didn’t require nightly, so I settled on iterators
in lieu of generators.
Macros
- Generates an iterator that yields the results of the comprehension. For more information, see
vec_comp!. - Performs a special comprehension that returns a
HashMap. This is different from the other comprehensions in that it requires two expressions: one for the key and one for the value. For more exhaustive documentation and usage examples, seevec_comp!. - Performs a comprehension and returns a
HashSetcontaining the results. For more information, seevec_comp!. - Performs a comprehension and returns a
Veccontaining the results. If you want the raw iterator, you can use theiter_comp!macro (that’s what this macro uses internally anyway).