pub struct TempDir { /* private fields */ }
Expand description
The path of an existing writable directory in a system temporary directory.
Drop the struct to delete the directory and everything under it. Deletes symbolic links and does not follow them.
Ignores any error while deleting.
See TempDir::panic_on_cleanup_error
.
Example
use temp_dir::TempDir;
let d = TempDir::new().unwrap();
// Prints "/tmp/t1a9b-0".
println!("{:?}", d.path());
let f = d.child("file1");
// Prints "/tmp/t1a9b-0/file1".
println!("{:?}", f);
std::fs::write(&f, b"abc").unwrap();
assert_eq!(
"abc",
std::fs::read_to_string(&f).unwrap(),
);
// Prints "/tmp/t1a9b-1".
println!("{:?}", TempDir::new().unwrap().path());
Implementations
sourceimpl TempDir
impl TempDir
sourcepub fn new() -> Result<Self, Error>
pub fn new() -> Result<Self, Error>
Create a new empty directory in a system temporary directory.
Drop the struct to delete the directory and everything under it. Deletes symbolic links and does not follow them.
Ignores any error while deleting.
See TempDir::panic_on_cleanup_error
.
Errors
Returns Err
when it fails to create the directory.
Example
// Prints "/tmp/t1a9b-0".
println!("{:?}", temp_dir::TempDir::new().unwrap().path());
sourcepub fn with_prefix(prefix: impl AsRef<str>) -> Result<Self, Error>
pub fn with_prefix(prefix: impl AsRef<str>) -> Result<Self, Error>
Create a new empty directory in a system temporary directory.
Use prefix
as the first part of the directory’s name.
Drop the struct to delete the directory and everything under it. Deletes symbolic links and does not follow them.
Ignores any error while deleting.
See TempDir::panic_on_cleanup_error
.
Errors
Returns Err
when it fails to create the directory.
Example
// Prints "/tmp/ok1a9b-0".
println!("{:?}", temp_dir::TempDir::with_prefix("ok").unwrap().path());
sourcepub fn cleanup(self) -> Result<(), Error>
pub fn cleanup(self) -> Result<(), Error>
Remove the directory on its contents now. Do nothing later on drop.
Errors
Returns an error if the directory exists and we fail to remove it and its contents.
sourcepub fn panic_on_cleanup_error(self) -> Self
pub fn panic_on_cleanup_error(self) -> Self
Make the struct panic on Drop if it hits an error while removing the directory or its contents.
Trait Implementations
sourceimpl Ord for TempDir
impl Ord for TempDir
sourceimpl PartialOrd<TempDir> for TempDir
impl PartialOrd<TempDir> for TempDir
sourcefn partial_cmp(&self, other: &TempDir) -> Option<Ordering>
fn partial_cmp(&self, other: &TempDir) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
impl Eq for TempDir
impl StructuralEq for TempDir
impl StructuralPartialEq for TempDir
Auto Trait Implementations
impl RefUnwindSafe for TempDir
impl Send for TempDir
impl Sync for TempDir
impl Unpin for TempDir
impl UnwindSafe for TempDir
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more