How to read file line by line

Read a file line by line in Rust using BufReader and the lines() iterator for efficient memory usage.

Use std::fs::File with BufReader and the lines() iterator to read a file line by line efficiently.

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

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

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