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
- Rust negative for loop example
- Rust parallel loop example
- How to do a for loop with index in Rust
- How to iterate string lines in Rust
- How to iterate in pairs in Rust
- How to iterate through hashmap keys in Rust
- How to iterate lines in file in Rust
- How to loop until error in Rust
- Rust for loop range inclusive example
- How to iterate hashmap in loop in Rust
More of Rust
- How to match whitespace with a regex in Rust?
- How to sort a Rust HashMap?
- How to replace a capture group using Rust regex?
- Rust parallel loop example
- How to use non-capturing groups in Rust regex?
- How to match a URL with a regex in Rust?
- How to replace strings using Rust regex?
- How to use negation in Rust regex?
- How to use regex lookahead in Rust?
- How to use a tuple as a key in a Rust HashMap?
See more codes...