Make a Custom Type Iterable

If a custom type is iterable, the for loop can be used to iterate through its items in sequence, as long as it has a type iterator registered.

Type iterators are already defined for built-in standard types such as strings, ranges, bit-fields, arrays and object maps. That’s why they can be used with the for loop.

Engine::register_iterator<T> allows registration of a type iterator for any type that implements IntoIterator.

With a type iterator registered, the custom type can be iterated through.

fn main() {
// Custom type
#[derive(Debug, Clone)]
struct TestStruct { fields: Vec<i64> }

// Implement 'IntoIterator' trait
impl IntoIterator<Item = i64> for TestStruct {
    type Item = i64;
    type IntoIter = std::vec::IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {

let mut engine = Engine::new();

// Register API and type iterator for 'TestStruct'
      .register_fn("new_ts", || TestStruct { fields: vec![1, 2, 3, 42] })
    // 'TestStruct' is iterable
    let ts = new_ts();

    // Use 'for' statement to loop through items in 'ts'
    for value in ts {