Rust:
Читання текстового файлу

How to: (Як це зробити:)

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

fn main() -> io::Result<()> {
    let mut file = File::open("hello.txt")?; // Open a file
    let mut contents = String::new();
    file.read_to_string(&mut contents)?; // Read file contents
    println!("File Contents:\n{}", contents);
    Ok(())
}

Output:

File Contents:
Hello, Ukraine!

Deep Dive (Поглиблений розбір)

Old days had it rough; reading files was low-level and cumbersome. Rust simplifies it, safely. Alternatives exist: read_to_string for all-at-once, BufReader for efficiency with large files. Under the hood, Rust leans on OS abstractions, ensuring safety and speed.

See Also (Дивись також):