Skip to content

Filter entity lists

factorio-rs lowers a small iterator subset to Lua. Use it when you already have a Vec (or a numeric range) and want a new table without writing nested for loops by hand.

Bindings typed as Vec<_> (or .iter()) use ipairs - order is preserved:

type Entities = Vec<LuaEntity>;
fn count_inserters(entities: Entities) -> i64 {
let mut n = 0;
for entity in entities {
if entity.name() == "inserter" {
n += 1;
}
}
n
}

Ranges become Lua numeric for:

for i in 0..n {
// i is 0 .. n-1
}
for i in 0..=n {
// inclusive end
}

Supported chain: range or Vec + .map and/or .filter + .collect (including .collect::<Vec<_>>()). Other adapters (enumerate, zip, …) are rejected.

fn double_positive(values: Vec<i64>) -> Vec<i64> {
values
.iter()
.map(|n| n * 2)
.filter(|n| *n > 0)
.collect::<Vec<_>>()
}
fn indices(n: i64) -> Vec<i64> {
(0..n).map(|i| i + 1).collect::<Vec<_>>()
}

That lowers to an immediately invoked Lua function that builds a table.

type Scores = Vec<i64>;
fn top_half(scores: Scores) -> Scores {
scores.iter().filter(|s| *s >= 50).collect::<Vec<_>>()
}

Aliases are transparent - see Type aliases.

Supported Not yet
for on Vec / .iter() / ranges enumerate, zip, flat_map
.map / .filter / .collect on that subset Standing .iter() without collect
type Name = Vec<_> for ipairs detection Arbitrary Iterator trait objects

Full table: Collections and iterators.