rustHow do I iterate over a string in Rust?
Iterating over a string in Rust is done using the chars()
method. This method returns an iterator over the characters of a string.
Example code
let my_string = "Hello World!";
for c in my_string.chars() {
println!("{}", c);
}
Output example
H
e
l
l
o
W
o
r
l
d
!
Code explanation
let my_string = "Hello World!";
: This line declares a string variable.for c in my_string.chars()
: This line starts a for loop that iterates over the characters of the string.println!("{}", c);
: This line prints each character of the string.
Helpful links
More of Rust
- How do I print the type of a variable in Rust?
- How to remove an element from a Rust HashMap if a condition is met?
- How to match whitespace with a regex in Rust?
- How to replace a capture group using Rust regex?
- How to match a URL with a regex in Rust?
- How to replace strings using Rust regex?
- How to parse a file with Rust regex?
- How to get a capture group using Rust regex?
- How to extract data with regex in Rust?
- How to escape dots with regex in Rust?
See more codes...