Python-style collection comprehensions in Rust
The structure is consistent with Python's list and dictionary comprehensions.
-
If the loop body (everything before the first
for
) contains two expressions separated by:
, it will be treated as a hash map comprehension. -
If the loop body ends with a
;
, it will be a statement comprehension (it won't evaluate to a collection). -
Otherwise it's a vector comprehension.
Comprehensions consist of a body followed by a for ... in ...
expression,
followed by any combination of for ... in ...
or if ...
expressions.
The for ... in ...
and if ...
expressions are nested left-to-right, so
c!;
is equivalent to:
for x in 0..10
Examples
Vectors
- A simple vector comprehension:
let v = c!;
println!;
[2, 6, 12, 20, 30, 42, 56, 72, 90, 110]
- Multiple iterators and conditionals:
let v = c!;
println!;
[-2, -1, 0, -6, -3, 0, 3, 6, -10, -5, 0, 5, 10, -14, -7, 0, 7, 14, -18, -9, 0, 9, 18]
Hash Maps
- A simple hash map comprehension:
let m = c!;
println!;
{2: 6, 5: 30, 8: 72, 4: 20, 3: 12, 6: 42, 1: 2, 7: 56, 9: 90, 10: 110}
- Complex hash map comprehension:
let m = c!;
println!;
{"[3|C]": "CCC", "[3|B]": "BBB", "[3|A]": "AAA", "[2|B]": "BB", "[1|C]": "C", "[1|A]": "A", "[1|B]": "B", "[2|C]": "CC", "[2|A]": "AA"}
Statements
- A simple statement comprehension:
let mut n = 1;
c!;
println!;
3628800