Skip to content
CryoCryo home

temporary

import std::fs::temporary; · source

Temporary files and directories, removed when they are dropped.

import std::fs::file::{ File };
import std::fs::temporary::{ TemporaryDirectory };

const scratch: TemporaryDirectory = TemporaryDirectory::create()?;
File::write_text(scratch.path().join("input.txt").as_path(), "data")?;
// ... the directory and everything in it is removed here

Each is created under env::temporary_directory() with a name of random hex digits from SecureRng, and created exclusively — O_EXCL / CREATE_NEW for a file, a failing mkdir for a directory — so two processes never share one. A name already taken is retried with another. keep hands the path over and leaves the entry in place.

Types

TemporaryDirectory

type struct TemporaryDirectory {
    private location: PathBuf;
    private kept:     boolean;
}

A new, empty directory, removed with everything in it when the value is dropped.

Constructors

static create() -> Result<TemporaryDirectory, IoError>;

A new, empty directory under the temporary directory.

Methods

path(&this) -> Path;

Where the directory is.

keep(mut this) -> PathBuf;

Leave the directory in place when this value is dropped, and return its path.

TemporaryFile

type struct TemporaryFile {
    private location: PathBuf;
    private handle:   File;
    private kept:     boolean;
}

A new, empty file, open for reading and writing, that is closed and removed when the value is dropped. file() borrows the open File to read, write, and seek; keep() closes it and leaves it in place.

import std::fs::temporary::{ TemporaryFile };

mut temp: TemporaryFile = TemporaryFile::create()?;
temp.file().write("scratch")?;

Constructors

static create() -> Result<TemporaryFile, IoError>;

A new, empty file under the temporary directory, open for reading and writing.

Methods

path(&this) -> Path;

Where the file is.

file(mut &this) -> mut &File;

The open file, to read, write and seek.

keep(mut this) -> PathBuf;

Leave the file in place when this value is dropped, and return its path. The file is still closed then.

Trait implementations

TemporaryDirectory

implement trait Drop for struct TemporaryDirectory

TemporaryFile

implement trait Drop for struct TemporaryFile