Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Split on newlines - use lines

  • lines

  • Lines

  • The lines method returns a Lines struct, an iterator over the lines.

fn main() {
    let text = "row 1\nrow 2\nrow 3\n";
    println!("{text}");
    println!("-----");

    let lines = text.lines();
    for line in lines {
        println!("line: {line}");
    }
}
row 1
row 2
row 3

-----
line: row 1
line: row 2
line: row 3