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
- How to iterate and modify a vector in Rust
- How to iterate in pairs in Rust
- How to sleep in a loop in Rust
- How to iterate over string in Rust
- Rust for loop range inclusive example
- How to loop N times in Rust
- How to iterate a map in Rust
- How to get index in for loop in Rust
More of Rust
- How to replace a capture group using Rust regex?
- How to map an array in Rust
- How to replace strings using Rust regex?
- How to parse a file with Rust regex?
- How to match the end of a line in a Rust regex?
- How to use regex to match a double quote in Rust?
- How to match whitespace with a regex in Rust?
- How to push an element to a Rust slice?
- How to escape dots with regex in Rust?
- How to split a string with Rust regex?
See more codes...