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 perform matrix operations in Rust?
- How to match whitespace with a regex in Rust?
- How to replace strings using Rust regex?
- How to insert an element into a Rust HashMap if it does not already exist?
- How to use regex lookbehind in Rust?
- Regex example to match multiline string in Rust?
- How to compare two Rust HashMaps?
- How to use regex lookahead in Rust?
- How to sort a Rust HashMap?
- How to declare a constant Rust HashMap?
See more codes...