How to Read a String Line by Line in Rust

Read a string line by line in Rust using the lines() iterator on a BufReader.

Use the lines() iterator on a File to process each line sequentially.

use std::fs::File;
use std::io::{self, BufRead};

fn main() -> io::Result<()> {
    let file = File::open("input.txt")?;
    let reader = io::BufReader::new(file);

    for line in reader.lines() {
        let line = line?;
        println!("{line}");
    }
    Ok(())
}