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
:
toml
[dependencies]
tempdir = "0.3"
and this to your crate root:
rust
extern crate tempdir;
This sample method does the following:
```rust use std::io::{self, Write}; use std::fs::File; use tempdir::TempDir;
fn writetempfolderwithfiles() -> io::Result<()> { let dir = TempDir::new("mydirectoryprefix")?; let filepath = dir.path().join("foo.txt"); println!("{:?}", filepath);
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.