Reputation: 3266
If I call .to_path_buf()
immediately after expect
, the temporary directory will not be created. Is this a bug or a Rust feature?
extern crate mktemp;
use std::path::Path;
fn main() {
let temp_dir = mktemp::Temp::new_dir().expect("Failed to create a temp directory");
let temp_dir_path = temp_dir.to_path_buf();
println!("tmp path exists: {}", Path::exists(&temp_dir_path));
let temp_dir_path = mktemp::Temp::new_dir().expect("Failed to create a temp directory").to_path_buf();
println!("tmp path exists: {}", Path::exists(&temp_dir_path));
}
Which outputs:
tmp path exists: true
tmp path exists: false
Upvotes: 3
Views: 258
Reputation: 59065
I don't know, but I wonder if there's something in the mktemp
documentation about this...
Once the variable goes out of scope, the underlying file system resource is removed.
You're not storing the Temp
in a variable, so it goes out of scope immediately. It's creating the directory and then immediately destroying it.
Upvotes: 10