29
29
//! extern crate tempdir;
30
30
//!
31
31
//! use std::fs::File;
32
- //! use std::io::Write;
32
+ //! use std::io::{self, Write} ;
33
33
//! use tempdir::TempDir;
34
34
//!
35
35
//! fn main() {
36
+ //! if let Err(_) = run() {
37
+ //! ::std::process::exit(1);
38
+ //! }
39
+ //! }
40
+ //!
41
+ //! fn run() -> Result<(), io::Error> {
36
42
//! // Create a directory inside of `std::env::temp_dir()`, named with
37
43
//! // the prefix "example".
38
- //! let tmp_dir = TempDir::new("example").expect("create temp dir") ;
44
+ //! let tmp_dir = TempDir::new("example")? ;
39
45
//! let file_path = tmp_dir.path().join("my-temporary-note.txt");
40
- //! let mut tmp_file = File::create(file_path).expect("create temp file") ;
41
- //! writeln!(tmp_file, "Brian was here. Briefly.").expect("write temp file") ;
46
+ //! let mut tmp_file = File::create(file_path)? ;
47
+ //! writeln!(tmp_file, "Brian was here. Briefly.")? ;
42
48
//!
43
49
//! // By closing the `TempDir` explicitly, we can check that it has
44
50
//! // been deleted successfully. If we don't close it explicitly,
45
51
//! // the directory will still be deleted when `tmp_dir` goes out
46
52
//! // of scope, but we won't know whether deleting the directory
47
53
//! // succeeded.
48
54
//! drop(tmp_file);
49
- //! tmp_dir.close().expect("delete temp dir");
55
+ //! tmp_dir.close()?;
56
+ //! Ok(())
50
57
//! }
51
58
//! ```
52
59
@@ -133,15 +140,19 @@ impl TempDir {
133
140
/// use std::io::Write;
134
141
/// use tempdir::TempDir;
135
142
///
143
+ /// # use std::io;
144
+ /// # fn run() -> Result<(), io::Error> {
136
145
/// // Create a directory inside of `std::env::temp_dir()`, named with
137
146
/// // the prefix, "example".
138
- /// let tmp_dir = TempDir::new("example").expect("create temp dir") ;
147
+ /// let tmp_dir = TempDir::new("example")? ;
139
148
/// let file_path = tmp_dir.path().join("my-temporary-note.txt");
140
- /// let mut tmp_file = File::create(file_path).expect("create temp file") ;
141
- /// writeln!(tmp_file, "Brian was here. Briefly.").expect("write temp file") ;
149
+ /// let mut tmp_file = File::create(file_path)? ;
150
+ /// writeln!(tmp_file, "Brian was here. Briefly.")? ;
142
151
///
143
152
/// // `tmp_dir` goes out of scope, the directory as well as
144
153
/// // `tmp_file` will be deleted here.
154
+ /// # Ok(())
155
+ /// # }
145
156
/// ```
146
157
pub fn new ( prefix : & str ) -> io:: Result < TempDir > {
147
158
TempDir :: new_in ( & env:: temp_dir ( ) , prefix)
@@ -163,12 +174,16 @@ impl TempDir {
163
174
/// use std::io::Write;
164
175
/// use tempdir::TempDir;
165
176
///
177
+ /// # use std::io;
178
+ /// # fn run() -> Result<(), io::Error> {
166
179
/// // Create a directory inside of the current directory, named with
167
180
/// // the prefix, "example".
168
- /// let tmp_dir = TempDir::new_in(".", "example").expect("create temp dir") ;
181
+ /// let tmp_dir = TempDir::new_in(".", "example")? ;
169
182
/// let file_path = tmp_dir.path().join("my-temporary-note.txt");
170
- /// let mut tmp_file = File::create(file_path).expect("create temp file");
171
- /// writeln!(tmp_file, "Brian was here. Briefly.").expect("write temp file");
183
+ /// let mut tmp_file = File::create(file_path)?;
184
+ /// writeln!(tmp_file, "Brian was here. Briefly.")?;
185
+ /// # Ok(())
186
+ /// # }
172
187
/// ```
173
188
pub fn new_in < P : AsRef < Path > > ( tmpdir : P , prefix : & str ) -> io:: Result < TempDir > {
174
189
let storage;
@@ -212,10 +227,12 @@ impl TempDir {
212
227
/// ```
213
228
/// use tempdir::TempDir;
214
229
///
230
+ /// # use std::io;
231
+ /// # fn run() -> Result<(), io::Error> {
215
232
/// let tmp_path;
216
233
///
217
234
/// {
218
- /// let tmp_dir = TempDir::new("example").expect("create temp dir") ;
235
+ /// let tmp_dir = TempDir::new("example")? ;
219
236
/// tmp_path = tmp_dir.path().to_owned();
220
237
///
221
238
/// // Check that the temp directory actually exists.
@@ -226,6 +243,8 @@ impl TempDir {
226
243
///
227
244
/// // Temp directory should be deleted by now
228
245
/// assert_eq!(tmp_path.exists(), false);
246
+ /// # Ok(())
247
+ /// # }
229
248
/// ```
230
249
pub fn path ( & self ) -> & path:: Path {
231
250
self . path . as_ref ( ) . unwrap ( )
@@ -243,14 +262,18 @@ impl TempDir {
243
262
/// use std::fs;
244
263
/// use tempdir::TempDir;
245
264
///
246
- /// let tmp_dir = TempDir::new("example").expect("create temp dir");
265
+ /// # use std::io;
266
+ /// # fn run() -> Result<(), io::Error> {
267
+ /// let tmp_dir = TempDir::new("example")?;
247
268
///
248
269
/// // Convert `tmp_dir` into a `Path`, destroying the `TempDir`
249
270
/// // without deleting the directory.
250
271
/// let tmp_path = tmp_dir.into_path();
251
272
///
252
273
/// // Delete the temporary directory ourselves.
253
- /// fs::remove_dir_all(tmp_path).expect("remove temp dir");
274
+ /// fs::remove_dir_all(tmp_path)?;
275
+ /// # Ok(())
276
+ /// # }
254
277
/// ```
255
278
pub fn into_path ( mut self ) -> PathBuf {
256
279
self . path . take ( ) . unwrap ( )
@@ -278,20 +301,24 @@ impl TempDir {
278
301
/// use std::io::Write;
279
302
/// use tempdir::TempDir;
280
303
///
304
+ /// # use std::io;
305
+ /// # fn run() -> Result<(), io::Error> {
281
306
/// // Create a directory inside of `std::env::temp_dir()`, named with
282
307
/// // the prefix, "example".
283
- /// let tmp_dir = TempDir::new("example").expect("create temp dir") ;
308
+ /// let tmp_dir = TempDir::new("example")? ;
284
309
/// let file_path = tmp_dir.path().join("my-temporary-note.txt");
285
- /// let mut tmp_file = File::create(file_path).expect("create temp file") ;
286
- /// writeln!(tmp_file, "Brian was here. Briefly.").expect("write temp file") ;
310
+ /// let mut tmp_file = File::create(file_path)? ;
311
+ /// writeln!(tmp_file, "Brian was here. Briefly.")? ;
287
312
///
288
313
/// // By closing the `TempDir` explicitly we can check that it has
289
314
/// // been deleted successfully. If we don't close it explicitly,
290
315
/// // the directory will still be deleted when `tmp_dir` goes out
291
316
/// // of scope, but we won't know whether deleting the directory
292
317
/// // succeeded.
293
318
/// drop(tmp_file);
294
- /// tmp_dir.close().expect("delete temp dir");
319
+ /// tmp_dir.close()?;
320
+ /// # Ok(())
321
+ /// # }
295
322
/// ```
296
323
pub fn close ( mut self ) -> io:: Result < ( ) > {
297
324
let result = fs:: remove_dir_all ( self . path ( ) ) ;
0 commit comments