diff options
| author | elszben <notgonna@tellyou> | 2014-12-16 19:15:05 +0100 |
|---|---|---|
| committer | elszben <notgonna@tellyou> | 2014-12-16 19:23:06 +0100 |
| commit | c0e8dc6dce5d847b7bf53b306d6d842e3a223c28 (patch) | |
| tree | 356b1842f4e2af1698f69df3ba1e3f28121b7067 /src/libstd | |
| parent | ef0bc464af110d24d4663fbe51eca3646a897308 (diff) | |
| download | rust-c0e8dc6dce5d847b7bf53b306d6d842e3a223c28.tar.gz rust-c0e8dc6dce5d847b7bf53b306d6d842e3a223c28.zip | |
Added example to TempDir
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/io/tempfile.rs | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/libstd/io/tempfile.rs b/src/libstd/io/tempfile.rs index f3a11939995..334ee9e1e1b 100644 --- a/src/libstd/io/tempfile.rs +++ b/src/libstd/io/tempfile.rs @@ -23,6 +23,59 @@ use sync::atomic; /// A wrapper for a path to temporary directory implementing automatic /// scope-based deletion. +/// +/// # Examples +/// +/// ``` +/// # fn main() {} +/// # fn foo () { +/// use std::io::TempDir; +/// +/// { +/// // create a temporary directory +/// let tmpdir = match TempDir::new("mysuffix") { +/// 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.as_str().unwrap()); +/// +/// // 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"), "mysuffix") { +/// 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.as_str().unwrap()); +/// +/// // the temporary directory is not removed here +/// // because the directory is detached from the wrapper +/// } +/// { +/// // create a temporary directory +/// let tmpdir = match TempDir::new("mysuffix") { +/// 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) +/// }; +/// } +/// # } +/// ``` pub struct TempDir { path: Option<Path>, disarmed: bool |
