rustHow do you enumerate a Rust string?
A Rust string can be enumerated using the chars()
method. This method returns an iterator over the characters of the string.
let s = "Hello World";
for c in s.chars() {
println!("{}", c);
}
Output example
H
e
l
l
o
W
o
r
l
d
The chars()
method returns an iterator over the characters of the string. The iterator can then be used to iterate over the characters of the string. In the example above, the iterator is used in a for loop to print out each character of the string.
Helpful links
More of Rust
- How to get a capture group using Rust regex?
- How to use regex with bytes in Rust?
- How to match the end of a line in a Rust regex?
- Hashshet example in Rust
- How to create a new Rust HashMap with values?
- How do I use a variable from another file in Rust?
- How to get the length of a Rust HashMap?
- How to get an element from a HashSet in Rust?
- How to implement PartialEq for a Rust HashMap?
- How to convert the keys of a Rust HashMap to a vector?
See more codes...