Into Iterator
The into_iter
step converts a walker traversal into a standard Rust iterator. This is a terminal step that consumes the walker and returns an iterator yielding the IDs of the vertices or edges that were in the traversal stream. It allows you to bridge the Graph API's walker system with Rust's standard iterator ecosystem.
In this diagram:
- Input Elements: The walker starts with elements A, B, C.
- The
.into_iter()
step processes the stream and consumes the walker. - Rust Iterator: The step returns a standard Rust iterator. This iterator yields the unique identifiers (IDs) of the elements that were processed (ID(A), ID(B), ID(C)).
- Terminates Walker: This step ends the Graph API walker chain. Subsequent operations must use standard Rust iterator methods.
Syntax
walker.into_iter()
Parameters
This step takes no parameters.
Return Value
Returns a standard Rust iterator that yields vertex or edge IDs from the traversal.
Examples
Basic Usage
Convert a traversal to an iterator and collect results:
// Basic iteration to collect IDs
let vertex_ids = graph
.walk()
.vertices(VertexSearch::scan())
.into_iter()
.collect::<Vec<_>>();
// There should be at least 4 vertices in the graph
assert!(vertex_ids.len() >= 4);
Filtering with Standard Iterators
Use standard Rust iterator methods:
// We can use standard iterator operations like filtering
let filtered_vertices = graph
.walk()
.vertices(VertexSearch::scan())
.into_iter()
.filter(|vertex_id| {
// Get the vertex reference from the ID
if let Some(vertex) = graph.vertex(*vertex_id) {
// Check if it's a Person
matches!(vertex.weight(), Vertex::Person { .. })
} else {
false
}
})
.collect::<Vec<_>>();
// There should be exactly 2 Person vertices (bryn and julia)
assert_eq!(filtered_vertices.len(), 2);
Comparing with Walker Methods
Walker methods vs standard iterator methods:
// Using .map() on the walker directly yields references with context
let vertex_names = graph
.walk()
.vertices(VertexSearch::scan())
.map(|vertex, _ctx| match vertex.weight() {
Vertex::Person { name, .. } => name.clone(),
Vertex::Project { name } => name.clone(),
_ => "Unknown".to_string(),
})
.collect::<Vec<_>>();
assert!(vertex_names.contains(&"Bryn".to_string()));
assert!(vertex_names.contains(&"Julia".to_string()));
assert!(vertex_names.contains(&"GraphApi".to_string()));
assert!(vertex_names.contains(&"Rust".to_string()));
Best Practices
- Use
into_iter
only when you need to leverage standard Rust iterators - Remember that standard iterator methods lose access to graph context
- Consider extracting essential data into context before converting to an iterator
- When working with large graphs, use the Graph API's lazy evaluation before converting to an iterator
Common Use Cases
- Integration with existing code: Bridging Graph API traversals with existing iterator-based systems
- Complex iterator chains: Using specialized iterator adaptors not available as walker steps
- ID-based operations: Working with element IDs directly for memory efficiency
- Ecosystem integration: Connecting graph traversals with Rust's extensive iterator ecosystem