pub struct TempDir { /* private fields */ }Expand description
A temporary directory that auto-deletes when dropped.
§Example
use mod_tempdir::TempDir;
let dir = TempDir::new().unwrap();
let file_path = dir.path().join("test.txt");
std::fs::write(&file_path, b"hello").unwrap();
// dir and its contents are deleted at end of scopeImplementations§
Source§impl TempDir
impl TempDir
Sourcepub fn new() -> Result<Self>
pub fn new() -> Result<Self>
Create a new temporary directory in the system’s temp location
(/tmp on Linux/macOS, %TEMP% on Windows).
The basename is .tmp-{pid}-{name12} where {pid} is the
current process ID (used by cleanup_orphans to identify
entries left behind by crashed processes) and {name12} is a
12-character Crockford base32 string from the shared name
generator. With the mod-rand feature enabled, the name
fragment comes from mod_rand::tier2::unique_name;
without it, from an internal process-unique mixer.
§Errors
Returns the underlying io::Error from
std::fs::create_dir if the directory cannot be created.
§Example
use mod_tempdir::TempDir;
let dir = TempDir::new().unwrap();
assert!(dir.path().is_dir());Examples found in repository?
7fn main() -> std::io::Result<()> {
8 let dir = TempDir::new()?;
9 println!("Created: {}", dir.path().display());
10
11 let file = dir.path().join("greeting.txt");
12 std::fs::write(&file, b"Hello from mod-tempdir!\n")?;
13 println!("Wrote: {}", file.display());
14
15 println!("Contents: {}", std::fs::read_to_string(&file)?);
16 println!("\n(directory will be deleted automatically when this program exits)");
17
18 Ok(())
19}Sourcepub fn with_prefix(prefix: &str) -> Result<Self>
pub fn with_prefix(prefix: &str) -> Result<Self>
Create a new temporary directory with the given prefix.
The final basename is {prefix}-{12-char-name}. The prefix is
joined verbatim and is the caller’s responsibility to sanitize.
§Errors
Returns the underlying io::Error from
std::fs::create_dir if the directory cannot be created.
§Example
use mod_tempdir::TempDir;
let dir = TempDir::with_prefix("my-app").unwrap();
assert!(dir
.path()
.file_name()
.unwrap()
.to_string_lossy()
.starts_with("my-app-"));Sourcepub fn path(&self) -> &Path
pub fn path(&self) -> &Path
Return the path of this temporary directory.
§Example
use mod_tempdir::TempDir;
let dir = TempDir::new().unwrap();
let log = dir.path().join("output.log");
std::fs::write(&log, b"hello").unwrap();Examples found in repository?
7fn main() -> std::io::Result<()> {
8 let dir = TempDir::new()?;
9 println!("Created: {}", dir.path().display());
10
11 let file = dir.path().join("greeting.txt");
12 std::fs::write(&file, b"Hello from mod-tempdir!\n")?;
13 println!("Wrote: {}", file.display());
14
15 println!("Contents: {}", std::fs::read_to_string(&file)?);
16 println!("\n(directory will be deleted automatically when this program exits)");
17
18 Ok(())
19}Sourcepub fn persist(self) -> PathBuf
pub fn persist(self) -> PathBuf
Consume this TempDir and return the path, disabling cleanup
on drop. The directory and its contents will persist.
Use this when you want to inspect contents after a test fails.
§Example
use mod_tempdir::TempDir;
let dir = TempDir::new().unwrap();
let kept = dir.persist();
// `kept` survives past the original `dir` going out of scope.Sourcepub fn cleanup_on_drop(&self) -> bool
pub fn cleanup_on_drop(&self) -> bool
Return true if the directory will be deleted on drop.
§Example
use mod_tempdir::TempDir;
let dir = TempDir::new().unwrap();
assert!(dir.cleanup_on_drop());