Iterators for Custom Types
If a custom type is iterable, the for
loop can be used to iterate through
its items in sequence.
In order to use a for
statement, a type iterator must be registered for
the custom type in question.
Engine::register_iterator<T>
allows registration of a type iterator for any type
that implements IntoIterator
:
#![allow(unused)] fn main() { // Custom type #[derive(Debug, Clone)] struct TestStruct { ... } // Implement 'IntoIterator' trait impl IntoIterator<Item = ...> for TestStruct { type Item = ...; type IntoIter = SomeIterType<Self::Item>; fn into_iter(self) -> Self::IntoIter { ... } } engine .register_type_with_name::<TestStruct>("TestStruct") .register_fn("new_ts", || TestStruct { ... }) .register_iterator::<TestStruct>(); // register type iterator }
With a type iterator registered, the custom type can be iterated through:
#![allow(unused)] fn main() { let ts = new_ts(); // Use 'for' statement to loop through items in 'ts' for item in ts { ... } }