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 do a for loop with index in Rust
- How to sleep in a loop in Rust
- How to iterate string lines in Rust
- How to get index in for loop in Rust
- Rust parallel loop example
- How to iterate and modify a vector in Rust
- Rust negative for loop example
- Rust named loop example
- How to iterate through hashmap values in Rust
- How to iterate a map in Rust
More of Rust
- How to match whitespace with a regex in Rust?
- How to use Unicode in a regex in Rust?
- How to use regex lookahead in Rust?
- How to match a URL with a regex in Rust?
- How to use non-capturing groups in Rust regex?
- How to replace all matches using Rust regex?
- How to find the first match in a Rust regex?
- How to print a Rust HashMap?
- How to use negation in Rust regex?
- How to extract data with regex in Rust?
See more codes...