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
- How to do a for loop with index in Rust
- How to sleep in a loop in Rust
- How to iterate a map in Rust
- How to iterate string lines in Rust
- Rust parallel loop example
- Rust negative for loop example
- Rust named loop example
- How to iterate throught JSON in Rust
- How to loop for backward in Rust
More of Rust
- How to replace strings using Rust regex?
- How to match whitespace with a regex in Rust?
- How to match a URL with a regex in Rust?
- Regex example to match multiline string in Rust?
- How to use Unicode in a regex in Rust?
- How to replace a capture group using Rust regex?
- How to extract data with regex in Rust?
- How to use regex to match a double quote in Rust?
- How to extend struct from another struct in Rust
- How to create a Rust HashMap from a vec?
See more codes...