std-method

File::sync_data: synchronize data

Request persistence of file contents without requiring all metadata to be synchronized. Some platforms implement it like sync_all. Use a fresh disposable working directory exclusively controlled by this program. Expected stdout (not executed): ok Common mistake: Assuming synchronization makes an entire multi-file operation transactional.

Formal Syntax & Structure

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

Working Example

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