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 variablecurrent
and assigns it to the head of the list.while let Some(node) = current
: Begins a loop that will continue untilcurrent
isNone
.println!("{}", node.data)
: Prints the data of the current node.current = node.next
: Assignscurrent
to the next node in the list.
Helpful links
Related
More of Rust
- Hashshet example in Rust
- How to convert Rust bytes to hex?
- How to convert a Rust HashMap to a JSON string?
- How to convert a Rust HashMap to JSON?
- How to get the last element of a Rust slice?
- How to use non-capturing groups in Rust regex?
- How to use groups in a Rust regex?
- How to match the end of a line in a Rust regex?
- How to escape dots with regex in Rust?
- How to use regex to match a group in Rust?
See more codes...