Struct std::old_io::TempDirUnstable
[-] [+]
[src]
pub struct TempDir { // some fields omitted }
A wrapper for a path to temporary directory implementing automatic scope-based deletion.
Examples
fn main() { use std::old_io::TempDir; { // create a temporary directory let tmpdir = match TempDir::new("myprefix") { Ok(dir) => dir, Err(e) => panic!("couldn't create temporary directory: {}", e) }; // get the path of the temporary directory without affecting the wrapper let tmppath = tmpdir.path(); println!("The path of temporary directory is {}", tmppath.display()); // the temporary directory is automatically removed when tmpdir goes // out of scope at the end of the block } { // create a temporary directory, this time using a custom path let tmpdir = match TempDir::new_in(&Path::new("/tmp/best/custom/path"), "myprefix") { Ok(dir) => dir, Err(e) => panic!("couldn't create temporary directory: {}", e) }; // get the path of the temporary directory and disable automatic deletion in the wrapper let tmppath = tmpdir.into_inner(); println!("The path of the not-so-temporary directory is {}", tmppath.display()); // the temporary directory is not removed here // because the directory is detached from the wrapper } { // create a temporary directory let tmpdir = match TempDir::new("myprefix") { Ok(dir) => dir, Err(e) => panic!("couldn't create temporary directory: {}", e) }; // close the temporary directory manually and check the result match tmpdir.close() { Ok(_) => println!("success!"), Err(e) => panic!("couldn't remove temporary directory: {}", e) }; } }use std::old_io::TempDir; { // create a temporary directory let tmpdir = match TempDir::new("myprefix") { Ok(dir) => dir, Err(e) => panic!("couldn't create temporary directory: {}", e) }; // get the path of the temporary directory without affecting the wrapper let tmppath = tmpdir.path(); println!("The path of temporary directory is {}", tmppath.display()); // the temporary directory is automatically removed when tmpdir goes // out of scope at the end of the block } { // create a temporary directory, this time using a custom path let tmpdir = match TempDir::new_in(&Path::new("/tmp/best/custom/path"), "myprefix") { Ok(dir) => dir, Err(e) => panic!("couldn't create temporary directory: {}", e) }; // get the path of the temporary directory and disable automatic deletion in the wrapper let tmppath = tmpdir.into_inner(); println!("The path of the not-so-temporary directory is {}", tmppath.display()); // the temporary directory is not removed here // because the directory is detached from the wrapper } { // create a temporary directory let tmpdir = match TempDir::new("myprefix") { Ok(dir) => dir, Err(e) => panic!("couldn't create temporary directory: {}", e) }; // close the temporary directory manually and check the result match tmpdir.close() { Ok(_) => println!("success!"), Err(e) => panic!("couldn't remove temporary directory: {}", e) }; }
Methods
impl TempDir
fn new_in(tmpdir: &Path, prefix: &str) -> IoResult<TempDir>
Attempts to make a temporary directory inside of tmpdir
whose name
will have the prefix prefix
. The directory will be automatically
deleted once the returned wrapper is destroyed.
If no directory can be created, Err
is returned.
fn new(prefix: &str) -> IoResult<TempDir>
Attempts to make a temporary directory inside of os::tmpdir()
whose
name will have the prefix prefix
. The directory will be automatically
deleted once the returned wrapper is destroyed.
If no directory can be created, Err
is returned.
fn into_inner(self) -> Path
Unwrap the wrapped std::path::Path
from the TempDir
wrapper.
This discards the wrapper so that the automatic deletion of the
temporary directory is prevented.
fn path<'a>(&'a self) -> &'a Path
Access the wrapped std::path::Path
to the temporary directory.
fn close(self) -> IoResult<()>
Close and remove the temporary directory
Although TempDir
removes the directory on drop, in the destructor
any errors are ignored. To detect errors cleaning up the temporary
directory, call close
instead.