std-method

str::lines

Iterate over lines separated by LF or CRLF, omitting the line terminators. A final terminator does not create an extra empty line. A lone CR is not a line ending for this method. Expected stdout (not executed): ["one", "two"] Common mistake: Assuming line endings are preserved in the returned slices.

Formal Syntax & Structure

rust syntaxrust
fn lines(&self) -> Lines<'_>

Working Example

Example Snippetrust
fn main() {    let lines: Vec<_> = "one\r\ntwo\n".lines().collect();    println!("{lines:?}");}
Rust Reference