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
- How to loop until error in Rust
- Rust for loop range inclusive example
- How to iterate a map in Rust
- How to iterate in pairs in Rust
- How to sleep in a loop in Rust
- How to iterate over string in Rust
- How to do a for loop with index in Rust
- How to iterate lines in file in Rust
- How to iterate and modify a vector in Rust
- How to iterate hashset in Rust
More of Rust
- How to match the end of a line in a Rust regex?
- How to replace a capture group using Rust regex?
- How to use non-capturing groups in Rust regex?
- How to get a capture group using Rust regex?
- How to find the first match in a Rust regex?
- Hashshet example in Rust
- How to get an entry from a HashSet in Rust?
- How to implement PartialEq for a Rust HashMap?
- How to use regex with bytes in Rust?
- Enum as u8 in Rust
See more codes...