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 parallel loop example
- Rust negative for loop example
- How to do a for loop with index in Rust
- How to iterate string lines in Rust
- How to loop until error in Rust
- How to sleep in a loop in Rust
- Rust for loop range inclusive example
- How to iterate in pairs in Rust
- How to iterate lines in file in Rust
- How to iterate hashset in Rust
More of Rust
- How to use regex lookahead in Rust?
- How to ignore case in Rust regex?
- How to insert an element into a Rust HashMap if it does not already exist?
- How to match whitespace with a regex in Rust?
- How to replace strings using Rust regex?
- How to use negation in Rust regex?
- How to use named capture groups in Rust regex?
- How to use regex lookbehind in Rust?
- How to use regex captures in Rust?
- How to push an element to a Rust slice?
See more codes...