17
17
//!
18
18
//! The [`TempDir`] type creates a directory on the file system that
19
19
//! is deleted once it goes out of scope. At construction, the
20
- //! `TempDir` creates a new directory with a randomly generated name,
21
- //! and with a prefix of your choosing.
20
+ //! `TempDir` creates a new directory with a randomly generated name
21
+ //! and a prefix of your choosing.
22
22
//!
23
23
//! [`TempDir`]: struct.TempDir.html
24
24
//! [`std::env::temp_dir()`]: https://doc.rust-lang.org/std/env/fn.temp_dir.html
34
34
//!
35
35
//! fn main() {
36
36
//! // Create a directory inside of `std::env::temp_dir()`, named with
37
- //! // the prefix, "example".
37
+ //! // the prefix "example".
38
38
//! let tmp_dir = TempDir::new("example").expect("create temp dir");
39
39
//! let file_path = tmp_dir.path().join("my-temporary-note.txt");
40
40
//! let mut tmp_file = File::create(file_path).expect("create temp file");
41
41
//! writeln!(tmp_file, "Brian was here. Briefly.").expect("write temp file");
42
42
//!
43
- //! // By closing the `TempDir` explicitly we can check that it has
43
+ //! // By closing the `TempDir` explicitly, we can check that it has
44
44
//! // been deleted successfully. If we don't close it explicitly,
45
45
//! // the directory will still be deleted when `tmp_dir` goes out
46
46
//! // of scope, but we won't know whether deleting the directory
@@ -68,12 +68,12 @@ use rand::{thread_rng, Rng};
68
68
/// and with a prefix of your choosing.
69
69
///
70
70
/// The default constructor, [`TempDir::new`], creates directories in
71
- /// the location returned by [`std::env::temp_dir()`], but they can be
72
- /// configured to manage the lifetime of a temporary directory in any
73
- /// location by constructing with [`TempDir::new_in`].
71
+ /// the location returned by [`std::env::temp_dir()`], but `TempDir`
72
+ /// can be configured to manage a temporary directory in any location
73
+ /// by constructing with [`TempDir::new_in`].
74
74
///
75
75
/// After creating a `TempDir`, work with the file system by doing
76
- /// standard [`std::fs`] file system operations on it's [`Path`],
76
+ /// standard [`std::fs`] file system operations on its [`Path`],
77
77
/// which can be retrieved with [`TempDir::path`]. Once the `TempDir`
78
78
/// value is dropped, the directory at the path will be deleted, along
79
79
/// with any files and directories it contains. It is your responsibility
@@ -122,6 +122,8 @@ impl TempDir {
122
122
/// everything inside it will be automatically deleted once the
123
123
/// returned `TempDir` is destroyed.
124
124
///
125
+ /// # Errors
126
+ ///
125
127
/// If the directory can not be created, `Err` is returned.
126
128
///
127
129
/// # Examples
@@ -137,6 +139,9 @@ impl TempDir {
137
139
/// let file_path = tmp_dir.path().join("my-temporary-note.txt");
138
140
/// let mut tmp_file = File::create(file_path).expect("create temp file");
139
141
/// writeln!(tmp_file, "Brian was here. Briefly.").expect("write temp file");
142
+ ///
143
+ /// // `tmp_dir` goes out of scope, the directory as well as
144
+ /// // `tmp_file` will be deleted here.
140
145
/// ```
141
146
pub fn new ( prefix : & str ) -> io:: Result < TempDir > {
142
147
TempDir :: new_in ( & env:: temp_dir ( ) , prefix)
@@ -147,6 +152,8 @@ impl TempDir {
147
152
/// everything inside it will be automatically deleted once the
148
153
/// returned `TempDir` is destroyed.
149
154
///
155
+ /// # Errors
156
+ ///
150
157
/// If the directory can not be created, `Err` is returned.
151
158
///
152
159
/// # Examples
@@ -205,10 +212,20 @@ impl TempDir {
205
212
/// ```
206
213
/// use tempdir::TempDir;
207
214
///
208
- /// let tmp_dir = TempDir::new("example").expect("create temp dir");
215
+ /// let tmp_path;
216
+ ///
217
+ /// {
218
+ /// let tmp_dir = TempDir::new("example").expect("create temp dir");
219
+ /// tmp_path = tmp_dir.path().to_owned();
209
220
///
210
- /// // Check that the temp directory actually exists.
211
- /// assert!(tmp_dir.path().exists());
221
+ /// // Check that the temp directory actually exists.
222
+ /// assert!(tmp_path.exists());
223
+ ///
224
+ /// // End of `tmp_dir` scope, directory will be deleted
225
+ /// }
226
+ ///
227
+ /// // Temp directory should be deleted by now
228
+ /// assert_eq!(tmp_path.exists(), false);
212
229
/// ```
213
230
pub fn path ( & self ) -> & path:: Path {
214
231
self . path . as_ref ( ) . unwrap ( )
@@ -245,6 +262,15 @@ impl TempDir {
245
262
/// any errors are ignored. To detect errors cleaning up the temporary
246
263
/// directory, call `close` instead.
247
264
///
265
+ /// # Errors
266
+ ///
267
+ /// This function may return a variety of [`std::io::Error`]s that result from deleting
268
+ /// the files and directories contained with the temporary directory,
269
+ /// as well as from deleting the temporary directory itself. These errors
270
+ /// may be platform specific.
271
+ ///
272
+ /// [`std::io::Error`]: http://doc.rust-lang.org/std/io/struct.Error.html
273
+ ///
248
274
/// # Examples
249
275
///
250
276
/// ```
0 commit comments