Crates.io | tempdir |
lib.rs | tempdir |
version | 0.3.7 |
source | src |
created_at | 2015-02-22 00:27:07.951648 |
updated_at | 2018-03-21 07:46:03.078845 |
description | A library for managing a temporary directory and deleting all contents when it's dropped. |
homepage | https://github.com/rust-lang/tempdir |
repository | https://github.com/rust-lang/tempdir |
max_upload_size | |
id | 1447 |
size | 36,256 |
A Rust library for creating a temporary directory and deleting its entire contents when the directory is dropped.
The tempdir
crate is being merged into tempfile
. Please see this issue to track progress and direct new issues and pull requests to tempfile
.
Add this to your Cargo.toml
:
[dependencies]
tempdir = "0.3"
and this to your crate root:
extern crate tempdir;
This sample method does the following:
use std::io::{self, Write};
use std::fs::File;
use tempdir::TempDir;
fn write_temp_folder_with_files() -> io::Result<()> {
let dir = TempDir::new("my_directory_prefix")?;
let file_path = dir.path().join("foo.txt");
println!("{:?}", file_path);
let mut f = File::create(file_path)?;
f.write_all(b"Hello, world!")?;
f.sync_all()?;
dir.close()?;
Ok(())
}
Note: Closing the directory is actually optional, as it would be done on drop. The benefit of closing here is that it allows possible errors to be handled.