try_new_tempfile

Function try_new_tempfile 

Source
pub fn try_new_tempfile(content: impl AsRef<str>) -> Result<NamedTempFile>
Expand description

Creates a new temporary file and writes the given content into it.

This function generates a temporary file using the system’s default temporary directory, writes the provided string content into it, and returns a handle to the file. The temporary file will be automatically deleted when dropped.

§Parameters

  • content: The string content to write into the temporary file. Accepts any type implementing AsRef<str>.

§Returns

  • An Result containing a NamedTempFile handle if the operation succeeds, or an error if it fails.

§Examples

use regd_testing;

let tempfile = regd_testing::io::try_new_tempfile("Temporary data")
    .expect("failed to create temporary file");

println!("Temp file path: {:?}", tempfile.path());