On this page
  1. File
  2. Signature & Overview
  3. Notes & Pitfalls
  4. Example

File

Prev: io_and_fs | Up: io_and_fs | Next: `BufReader`
Rel: io_and_fs, rust/builtins
Ref: Rust std — File
Tags: #public #prog #rust #stdlib #io_and_fs #tier/core


Signature & Overview

std::fs::File  /* + std::fs::read_to_string */

OS file handle implementing Read and Write, automatically closed on Drop.

Notes & Pitfalls

Example

use std::fs;
use std::io::Write;

fn main() -> std::io::Result<()> {
    let path = std::env::temp_dir().join("rust_file_demo.txt");
    {
        let mut f = fs::File::create(&path)?;
        writeln!(f, "hello rust")?;
    }
    assert_eq!(fs::read_to_string(&path)?.trim(), "hello rust");
    fs::remove_file(path)?;
    Ok(())
}

Linked from

Related notes