rustHow to iterate hashset in Rust
Iterating over a HashSet in Rust can be done using the iter() method. This method returns an iterator over the elements of the set.
Example code
let mut set = HashSet::new();
set.insert("foo");
set.insert("bar");
for item in set.iter() {
    println!("{}", item);
}Output example
foo
barCode explanation
- let mut set = HashSet::new();: creates a new empty- HashSet
- set.insert("foo");: inserts the string- "foo"into the- HashSet
- set.insert("bar");: inserts the string- "bar"into the- HashSet
- for item in set.iter() {: starts an iterator over the elements of the- HashSet
- println!("{}", item);: prints the current element of the- HashSet
- }: ends the iterator
Helpful links
Related
More of Rust
- How to replace a capture group using Rust regex?
- How to use non-capturing groups in Rust regex?
- Regex example to match multiline string in Rust?
- How do I identify unused variables in Rust?
- How to compare enum in Rust
- YAML serde example in Rust
- How to use regex lookbehind in Rust?
- How to match the end of a line in a Rust regex?
- How to replace strings using Rust regex?
- How to match whitespace with a regex in Rust?
See more codes...