std-method

File::sync_all: request file synchronization

Ask the OS to synchronize file contents and metadata. Check errors; a successful file sync is not proof of transaction-level durability or persistence of a directory rename. Run only inside a disposable working directory. The fixture must not already exist. Expected stdout (not executed): saved Common mistake: Equating flush, file sync and a complete crash-safe replacement protocol.

Formal Syntax & Structure

rust syntaxrust
file.sync_all() -> io::Result<()>

Working Example

Example Snippetrust
fn main() {    use std::fs::{self, File, OpenOptions};    use std::io::{Read, Write};    fs::create_dir("bm-fixture").expect("use a fresh disposable directory");    let p = std::path::Path::new("bm-fixture").join("note.txt");    let mut f = File::create_new(&p).unwrap();    f.write_all(b"saved").unwrap();    f.sync_all().unwrap();    println!("{}", fs::read_to_string(&p).unwrap());}
Rust Reference