try_new_file

Function try_new_file 

Source
pub fn try_new_file(
    path: impl AsRef<Path>,
    content: impl AsRef<str>,
) -> Result<File>
Expand description

Creates a new file at the specified path and writes the given content to it.

This function attempts to create a file at the provided path, writes the specified string content to it, and flushes the buffer to ensure all data is persisted to disk.

§Parameters

  • path: The target path for the new file. Accepts any type implementing AsRef<Path>.
  • content: The string content to write into the newly created file. Accepts any type implementing AsRef<str>.

§Returns

  • An Result containing the created File handle if successful, or an error if file creation or writing fails.

§Examples

use regd_testing;

let file = regd_testing::io::try_new_file("output.txt", "Hello, world!")
    .expect("failed to create or write to file");