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

Create a new empty file in a system temporary directory.

Drop the returned struct to delete the file.

Errors

Returns Err when it fails to create the file.

Example
// Prints "/tmp/1a9b0".
println!("{:?}", temp_file::TempFile::new().unwrap().path());

Create a new empty file in dir.

Drop the returned struct to delete the file.

Errors

Returns Err when it fails to create the file.

Example
// Prints "/tmp/temp_uploads/1a9b0".
let dir = std::env::temp_dir().join("temp_uploads");
println!("{:?}", temp_file::TempFile::in_dir(&dir).unwrap().path());

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());

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());

Write contents to the file.

Errors

Returns Err when it fails to write all of contents to the file.

Remove the file now. Do nothing later on drop.

Errors

Returns an error if the file exists and we fail to remove it.

Make the struct panic on drop if it hits an error while removing the file.

Do not delete the file.

This is useful when debugging a test.

The path to the file.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Executes the destructor for this type. Read more

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.