write_atomic
Writes contents to path so that readers see either the old file or the new one, never a
half-written one.
The data goes to a temporary file in the same directory (so the last step stays on one file
system), is flushed to disk, and is then renamed over path. If anything fails the temporary
file is removed and path is left as it was. The parent directory must exist.
Import
Section titled “Import”Cargo feature fs (enabled by default). To compile only this module:
or in Cargo.toml:
Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
path | impl AsRef<Path> | The file to create or replace. |
contents | impl AsRef<[u8]> | The bytes to write. |
Returns
Section titled “Returns”io::Result<()>
Errors
Section titled “Errors”An io::Error when path has no file name, the temporary file cannot be created or
written, or the final rename fails (for instance because path is a directory).
