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
More of Rust
- How to replace a capture group using Rust regex?
- How to match whitespace with a regex in Rust?
- How to use regex captures in Rust?
- How to use binary regex in Rust?
- How to use regex to match a double quote in Rust?
- How to perform matrix operations in Rust?
- How to lock a Rust HashMap?
- How to compare two Rust HashMaps?
- How to convert a Rust slice of u8 to a string?
- How to iterate over a Rust HashMap?
See more codes...