Skip to content

Commit 1521511

Browse files
committed
2.8 Q1 を lib::env::temp_file を使って書き換え
1 parent 8617182 commit 1521511

File tree

1 file changed

+2
-18
lines changed

1 file changed

+2
-18
lines changed

chapter2/src/2_8_1/main.rs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,12 @@
11
//! Write format string to a file using `write!` macro (Go version uses `Fprintf`).
22
33
use std::{
4-
env::temp_dir,
54
fmt,
65
fs::File,
76
io::{self, Read, Write},
8-
path::PathBuf,
9-
time::{SystemTime, UNIX_EPOCH},
107
};
118

12-
fn mktemp() -> PathBuf {
13-
let mut tempdir = temp_dir();
14-
15-
let tempfile = {
16-
let now = SystemTime::now();
17-
let unixtime = now
18-
.duration_since(UNIX_EPOCH)
19-
.expect("system clock maybe corrupt");
20-
format!("{}-{:09}", unixtime.as_secs(), unixtime.subsec_nanos())
21-
};
22-
23-
tempdir.push(tempfile);
24-
tempdir
25-
}
9+
use lib::env::temp_file;
2610

2711
fn write_fmt<W: fmt::Write>(w: &mut W) {
2812
write!(
@@ -34,7 +18,7 @@ fn write_fmt<W: fmt::Write>(w: &mut W) {
3418
}
3519

3620
fn main() -> io::Result<()> {
37-
let tmp_path = mktemp();
21+
let tmp_path = temp_file();
3822
{
3923
let mut contents = String::new();
4024
write_fmt(&mut contents);

0 commit comments

Comments
 (0)