Skip to content
This repository was archived by the owner on Aug 20, 2021. It is now read-only.

Commit ed7a0b4

Browse files
authored
Merge pull request #29 from pwoolcoc/issue-23
Doc fixes for try! and .expect
2 parents 1708bf0 + 5afc695 commit ed7a0b4

File tree

2 files changed

+49
-22
lines changed

2 files changed

+49
-22
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ fn write_temp_folder_with_files() -> Result<(), io::Error> {
3939
let file_path = dir.path().join("foo.txt");
4040
println!("{:?}", file_path);
4141

42-
let mut f = try!(File::create(file_path));
43-
try!(f.write_all(b"Hello, world!"));
44-
try!(f.sync_all());
45-
try!(dir.close());
42+
let mut f = File::create(file_path)?;
43+
f.write_all(b"Hello, world!")?;
44+
f.sync_all()?;
45+
dir.close()?;
4646
}
4747
Ok(())
4848
}

src/lib.rs

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,31 @@
2929
//! extern crate tempdir;
3030
//!
3131
//! use std::fs::File;
32-
//! use std::io::Write;
32+
//! use std::io::{self, Write};
3333
//! use tempdir::TempDir;
3434
//!
3535
//! fn main() {
36+
//! if let Err(_) = run() {
37+
//! ::std::process::exit(1);
38+
//! }
39+
//! }
40+
//!
41+
//! fn run() -> Result<(), io::Error> {
3642
//! // Create a directory inside of `std::env::temp_dir()`, named with
3743
//! // the prefix "example".
38-
//! let tmp_dir = TempDir::new("example").expect("create temp dir");
44+
//! let tmp_dir = TempDir::new("example")?;
3945
//! 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.")?;
4248
//!
4349
//! // By closing the `TempDir` explicitly, we can check that it has
4450
//! // been deleted successfully. If we don't close it explicitly,
4551
//! // the directory will still be deleted when `tmp_dir` goes out
4652
//! // of scope, but we won't know whether deleting the directory
4753
//! // succeeded.
4854
//! drop(tmp_file);
49-
//! tmp_dir.close().expect("delete temp dir");
55+
//! tmp_dir.close()?;
56+
//! Ok(())
5057
//! }
5158
//! ```
5259
@@ -133,15 +140,19 @@ impl TempDir {
133140
/// use std::io::Write;
134141
/// use tempdir::TempDir;
135142
///
143+
/// # use std::io;
144+
/// # fn run() -> Result<(), io::Error> {
136145
/// // Create a directory inside of `std::env::temp_dir()`, named with
137146
/// // the prefix, "example".
138-
/// let tmp_dir = TempDir::new("example").expect("create temp dir");
147+
/// let tmp_dir = TempDir::new("example")?;
139148
/// 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.")?;
142151
///
143152
/// // `tmp_dir` goes out of scope, the directory as well as
144153
/// // `tmp_file` will be deleted here.
154+
/// # Ok(())
155+
/// # }
145156
/// ```
146157
pub fn new(prefix: &str) -> io::Result<TempDir> {
147158
TempDir::new_in(&env::temp_dir(), prefix)
@@ -163,12 +174,16 @@ impl TempDir {
163174
/// use std::io::Write;
164175
/// use tempdir::TempDir;
165176
///
177+
/// # use std::io;
178+
/// # fn run() -> Result<(), io::Error> {
166179
/// // Create a directory inside of the current directory, named with
167180
/// // the prefix, "example".
168-
/// let tmp_dir = TempDir::new_in(".", "example").expect("create temp dir");
181+
/// let tmp_dir = TempDir::new_in(".", "example")?;
169182
/// 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+
/// # }
172187
/// ```
173188
pub fn new_in<P: AsRef<Path>>(tmpdir: P, prefix: &str) -> io::Result<TempDir> {
174189
let storage;
@@ -212,10 +227,12 @@ impl TempDir {
212227
/// ```
213228
/// use tempdir::TempDir;
214229
///
230+
/// # use std::io;
231+
/// # fn run() -> Result<(), io::Error> {
215232
/// let tmp_path;
216233
///
217234
/// {
218-
/// let tmp_dir = TempDir::new("example").expect("create temp dir");
235+
/// let tmp_dir = TempDir::new("example")?;
219236
/// tmp_path = tmp_dir.path().to_owned();
220237
///
221238
/// // Check that the temp directory actually exists.
@@ -226,6 +243,8 @@ impl TempDir {
226243
///
227244
/// // Temp directory should be deleted by now
228245
/// assert_eq!(tmp_path.exists(), false);
246+
/// # Ok(())
247+
/// # }
229248
/// ```
230249
pub fn path(&self) -> &path::Path {
231250
self.path.as_ref().unwrap()
@@ -243,14 +262,18 @@ impl TempDir {
243262
/// use std::fs;
244263
/// use tempdir::TempDir;
245264
///
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")?;
247268
///
248269
/// // Convert `tmp_dir` into a `Path`, destroying the `TempDir`
249270
/// // without deleting the directory.
250271
/// let tmp_path = tmp_dir.into_path();
251272
///
252273
/// // 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+
/// # }
254277
/// ```
255278
pub fn into_path(mut self) -> PathBuf {
256279
self.path.take().unwrap()
@@ -278,20 +301,24 @@ impl TempDir {
278301
/// use std::io::Write;
279302
/// use tempdir::TempDir;
280303
///
304+
/// # use std::io;
305+
/// # fn run() -> Result<(), io::Error> {
281306
/// // Create a directory inside of `std::env::temp_dir()`, named with
282307
/// // the prefix, "example".
283-
/// let tmp_dir = TempDir::new("example").expect("create temp dir");
308+
/// let tmp_dir = TempDir::new("example")?;
284309
/// 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.")?;
287312
///
288313
/// // By closing the `TempDir` explicitly we can check that it has
289314
/// // been deleted successfully. If we don't close it explicitly,
290315
/// // the directory will still be deleted when `tmp_dir` goes out
291316
/// // of scope, but we won't know whether deleting the directory
292317
/// // succeeded.
293318
/// drop(tmp_file);
294-
/// tmp_dir.close().expect("delete temp dir");
319+
/// tmp_dir.close()?;
320+
/// # Ok(())
321+
/// # }
295322
/// ```
296323
pub fn close(mut self) -> io::Result<()> {
297324
let result = fs::remove_dir_all(self.path());

0 commit comments

Comments
 (0)