rustHow to iterate linked list in Rust
Iterating a linked list in Rust is done using a loop and the .next() method. The loop will continue until the .next() method returns None.
let mut current = list.head;
while let Some(node) = current {
println!("{}", node.data);
current = node.next;
}
Code explanation
let mut current = list.head: Declares a mutable variablecurrentand assigns it to the head of the list.while let Some(node) = current: Begins a loop that will continue untilcurrentisNone.println!("{}", node.data): Prints the data of the current node.current = node.next: Assignscurrentto the next node in the list.
Helpful links
Related
- How to loop until error in Rust
- Rust parallel loop example
- How to iterate directory recursively in Rust
- How to iterate over ndarray rows in Rust
- How to iterate hashset in Rust
- How to iterate btreemap in Rust
- How to sleep in a loop in Rust
- Rust for loop range inclusive example
- How to iterate in pairs in Rust
More of Rust
- How to replace a capture group using Rust regex?
- How to parse a file with Rust regex?
- How to use Unicode in a regex in Rust?
- YAML serde example in Rust
- How to match whitespace with a regex in Rust?
- How to replace strings using Rust regex?
- How to split a string with Rust regex?
- How to replace all matches using Rust regex?
- How to use non-capturing groups in Rust regex?
- How to use negation in Rust regex?
See more codes...