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 in pairs in Rust
- How to iterate over string in Rust
- How to loop N times in Rust
- How to do a for loop with index in Rust
- How to iterate and modify a vector in Rust
- How to iterate directory in Rust
- Rust parallel loop example
More of Rust
- How to use regex to match a group in Rust?
- How to use regex to match a double quote in Rust?
- How to parse JSON string in Rust?
- How to convert a Rust HashMap to a BTreeMap?
- How to replace strings using Rust regex?
- Regex example to match multiline string in Rust?
- How to get a capture group using Rust regex?
- How to use groups in a Rust regex?
- How to split a string by regex in Rust?
- How to convert a u8 slice to a hex string in Rust?
See more codes...