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

Commit 1093b56

Browse files
authored
Merge pull request #8 from chills42/master
Provide a sample method in the readme
2 parents b460a2c + 9da4506 commit 1093b56

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,32 @@ and this to your crate root:
2222
```rust
2323
extern crate tempdir;
2424
```
25+
26+
## Example
27+
28+
This sample method does the following:
29+
30+
1. Create a temporary directory in the default location with the given prefix.
31+
2. Determine a file path in the directory and print it out.
32+
3. Create a file inside the temp folder.
33+
4. Write to the file and sync it to disk.
34+
5. Close the directory, deleting the contents in the process.
35+
36+
```rust
37+
fn write_temp_folder_with_files() -> Result<(), io::Error> {
38+
if let Ok(dir) = TempDir::new("my_directory_prefix") {
39+
let file_path = dir.path().join("foo.txt");
40+
println!("{:?}", file_path);
41+
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());
46+
}
47+
Ok(())
48+
}
49+
```
50+
51+
**Note:** Closing the directory is actually optional, as it would be done on
52+
drop. The benefit of closing here is that it allows possible errors to be
53+
handled.

0 commit comments

Comments
 (0)