pub struct TempFile { /* private fields */ }
Expand description
The path of an existing writable file in a system temporary directory.
Deletes the file on drop. Ignores errors deleting the file.
Example
use temp_file::TempFile;
let t = TempFile::new()
.unwrap()
.with_contents(b"abc")
.unwrap();
// Prints "/tmp/1a9b0".
println!("{:?}", t.path());
assert_eq!(
"abc",
std::fs::read_to_string(t.path()).unwrap(),
);
// Prints "/tmp/1a9b1".
println!("{:?}", TempFile::new().unwrap().path());
Implementations
sourceimpl TempFile
impl TempFile
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 file in a system temporary directory.
Use prefix
as the first part of the file’s name.
Drop the returned struct to delete the file.
Errors
Returns Err
when it fails to create the file.
Example
// Prints "/tmp/ok1a9b0".
println!("{:?}", temp_file::TempFile::with_prefix("ok").unwrap().path());
sourcepub fn with_suffix(suffix: impl AsRef<str>) -> Result<Self, Error>
pub fn with_suffix(suffix: impl AsRef<str>) -> Result<Self, Error>
Create a new empty file in a system temporary directory.
Use suffix
as the last part of the file’s name.
You can use this to give the filename a particular extension.
Drop the returned struct to delete the file.
Errors
Returns Err
when it fails to create the file.
Example
// Prints "/tmp/1a9b0.txt".
println!("{:?}", temp_file::TempFile::with_suffix(".txt").unwrap().path());
sourcepub fn cleanup(self) -> Result<(), Error>
pub fn cleanup(self) -> Result<(), Error>
Remove the file now. Do nothing later on drop.
Errors
Returns an error if the file exists and we fail to remove it.
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 file.
Trait Implementations
sourceimpl Ord for TempFile
impl Ord for TempFile
sourceimpl PartialOrd<TempFile> for TempFile
impl PartialOrd<TempFile> for TempFile
sourcefn partial_cmp(&self, other: &TempFile) -> Option<Ordering>
fn partial_cmp(&self, other: &TempFile) -> 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 TempFile
impl StructuralEq for TempFile
impl StructuralPartialEq for TempFile
Auto Trait Implementations
impl RefUnwindSafe for TempFile
impl Send for TempFile
impl Sync for TempFile
impl Unpin for TempFile
impl UnwindSafe for TempFile
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
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more