Crate rustcomp

source ·
Expand description

Adds vector, set, map, and iterator comprehensions to Rust. This is achieved through various functional macros, which are:

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, see vec_comp!.
  • Performs a comprehension and returns a HashSet containing the results. For more information, see vec_comp!.
  • Performs a comprehension and returns a Vec containing the results. If you want the raw iterator, you can use the iter_comp! macro (that’s what this macro uses internally anyway).