pub struct TempFile { /* private fields */ }Expand description
Temporary file removed when dropped.
Implementations§
Source§impl TempFile
impl TempFile
Sourcepub fn with_content(prefix: &str, content: &str) -> Result<Self>
pub fn with_content(prefix: &str, content: &str) -> Result<Self>
Create a temporary file with content.
Examples found in repository?
examples/quick_start.rs (line 22)
5fn main() -> std::io::Result<()> {
6 let root = std::env::temp_dir().join("mi_simple_io_quick_start");
7 if root.exists() {
8 remove(&root)?;
9 }
10 create_dir(&root)?;
11
12 let log = root.join("hello.txt");
13 write_file(&log, "Hello, Rust!\n")?;
14 append_file(&log, "Simple IO makes common tasks short.\n")?;
15 // Print file path
16 println!("File path: {}", log.display());
17 println!("{}", read_file(&log)?);
18 for line in read_all_lines(&log)? {
19 println!("line: {line}");
20 }
21
22 let temp = TempFile::with_content("quick_start", "temporary data")?;
23 println!("temp: {}", temp.path().display());
24
25 let stats = file_stats(&log)?;
26 println!(
27 "size={} lines={} words={} chars={}",
28 stats.size, stats.lines, stats.words, stats.chars
29 );
30
31 // remove(root)?;
32 Ok(())
33}Sourcepub fn path(&self) -> &Path
pub fn path(&self) -> &Path
Return the temporary file path.
Examples found in repository?
examples/quick_start.rs (line 23)
5fn main() -> std::io::Result<()> {
6 let root = std::env::temp_dir().join("mi_simple_io_quick_start");
7 if root.exists() {
8 remove(&root)?;
9 }
10 create_dir(&root)?;
11
12 let log = root.join("hello.txt");
13 write_file(&log, "Hello, Rust!\n")?;
14 append_file(&log, "Simple IO makes common tasks short.\n")?;
15 // Print file path
16 println!("File path: {}", log.display());
17 println!("{}", read_file(&log)?);
18 for line in read_all_lines(&log)? {
19 println!("line: {line}");
20 }
21
22 let temp = TempFile::with_content("quick_start", "temporary data")?;
23 println!("temp: {}", temp.path().display());
24
25 let stats = file_stats(&log)?;
26 println!(
27 "size={} lines={} words={} chars={}",
28 stats.size, stats.lines, stats.words, stats.chars
29 );
30
31 // remove(root)?;
32 Ok(())
33}Trait Implementations§
Auto Trait Implementations§
impl Freeze for TempFile
impl RefUnwindSafe for TempFile
impl Send for TempFile
impl Sync for TempFile
impl Unpin for TempFile
impl UnsafeUnpin for TempFile
impl UnwindSafe for TempFile
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more