rustHow to iterate string lines in Rust
Iterating over string lines in Rust can be done using the lines() method on a String type. This method returns an iterator over the lines of the string.
Example code
let text = "Hello
World";
for line in text.lines() {
println!("{}", line);
}
Output example
Hello
World
Code explanation
let text = "Hello\nWorld";: This creates aStringtype with two lines.for line in text.lines(): This creates an iterator over the lines of thetextstring.println!("{}", line);: This prints each line of thetextstring.
Helpful links
Related
More of Rust
- How to use regex lookahead in Rust?
- How to perform matrix operations in Rust?
- How to match whitespace with a regex in Rust?
- How to use regex to match a double quote in Rust?
- Yield example in Rust
- How to replace strings using Rust regex?
- How to create a HashMap of structs in Rust?
- How to sort a Rust HashMap?
- How to match a URL with a regex in Rust?
- How to use regex lookbehind in Rust?
See more codes...