mpvrc/tempfile/struct.TempDir.html

213 lines
No EOL
45 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="A directory in the filesystem that is automatically deleted when it goes out of scope."><title>TempDir in tempfile - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../static.files/rustdoc-42caa33d.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="tempfile" data-themes="" data-resource-suffix="" data-rustdoc-version="1.84.1 (e71f9a9a9 2025-01-27)" data-channel="1.84.1" data-search-js="search-92e6798f.js" data-settings-js="settings-0f613d39.js" ><script src="../static.files/storage-59e33391.js"></script><script defer src="sidebar-items.js"></script><script defer src="../static.files/main-5f194d8c.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-893ab5e7.css"></noscript><link rel="icon" href="https://www.rust-lang.org/favicon.ico"></head><body class="rustdoc struct"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle" title="show sidebar"></button><a class="logo-container" href="../tempfile/index.html"><img src="https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png" alt=""></a></nav><nav class="sidebar"><div class="sidebar-crate"><a class="logo-container" href="../tempfile/index.html"><img src="https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png" alt="logo"></a><h2><a href="../tempfile/index.html">tempfile</a><span class="version">3.14.0</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">TempDir</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#resource-leaking" title="Resource Leaking">Resource Leaking</a></li><li><a href="#examples" title="Examples">Examples</a></li></ul><h3><a href="#implementations">Methods</a></h3><ul class="block method"><li><a href="#method.close" title="close">close</a></li><li><a href="#method.into_path" title="into_path">into_path</a></li><li><a href="#method.new" title="new">new</a></li><li><a href="#method.new_in" title="new_in">new_in</a></li><li><a href="#method.path" title="path">path</a></li><li><a href="#method.with_prefix" title="with_prefix">with_prefix</a></li><li><a href="#method.with_prefix_in" title="with_prefix_in">with_prefix_in</a></li><li><a href="#method.with_suffix" title="with_suffix">with_suffix</a></li><li><a href="#method.with_suffix_in" title="with_suffix_in">with_suffix_in</a></li></ul><h3><a href="#trait-implementations">Trait Implementations</a></h3><ul class="block trait-implementation"><li><a href="#impl-AsRef%3CPath%3E-for-TempDir" title="AsRef&#60;Path&#62;">AsRef&#60;Path&#62;</a></li><li><a href="#impl-Debug-for-TempDir" title="Debug">Debug</a></li><li><a href="#impl-Drop-for-TempDir" title="Drop">Drop</a></li></ul><h3><a href="#synthetic-implementations">Auto Trait Implementations</a></h3><ul class="block synthetic-implementation"><li><a href="#impl-Freeze-for-TempDir" title="Freeze">Freeze</a></li><li><a href="#impl-RefUnwindSafe-for-TempDir" title="RefUnwindSafe">RefUnwindSafe</a></li><li><a href="#impl-Send-for-TempDir" title="Send">Send</a></li><li><a href="#impl-Sync-for-TempDir" title="Sync">Sync</a></li><li><a href="#impl-Unpin-for-TempDir" title="Unpin">Unpin</a></li><li><a href="#impl-UnwindSafe-for-TempDir" title="UnwindSafe">UnwindSafe</a></li></ul><h3><a href="#blanket-implementations">Blanket Implementations</a></h3><ul class="block blanket-implementation"><li><a href="#impl-Any-for-T" title="Any">Any</a></li><li><a href="#impl-Borrow%3CT%3E-for-T" title="Borrow&#60;T&#62;">Borrow&#60;T&#62;</a></li><li><a href="#impl-BorrowMut%3CT%3E-for-T" title="BorrowMut&#60;T&#62;">BorrowMut&#60;T&#62;</a></li><li><a href="#impl-From%3CT%3E-for-T" title="From&#60;T&#62;">From&#60;T&#62;</a></li><li><a href="#impl-Into%3CU%3E-for-T" title="Into&#60;U&#62;">Into&#60;U&#62;</a></li><li><a href="#impl-TryFrom%3CU%3E-for-T" title="TryFrom&#60;U&#62;">TryFrom&#60;U&#62;</a></li><li><a href="#impl-TryInto%3CU%3E-for-T" title="TryInto&#60;U&#62;">TryInto&#60;U&#62;</a></li></ul></section><div id="rustdoc-modnav"><h2 class="in-crate"><a href="index.html">In crate tempfile</a></h2></div></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><span class="rustdoc-breadcrumbs"><a href="index.html">tempfile</a></span><h1>Struct <span class="struct">TempDir</span><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../src/tempfile/dir/mod.rs.html#178-181">Source</a> </span></div><pre class="rust item-decl"><code>pub struct TempDir { <span class="comment">/* private fields */</span> }</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>A directory in the filesystem that is automatically deleted when
it goes out of scope.</p>
<p>The <a href="struct.TempDir.html"><code>TempDir</code></a> type creates a directory on the file system that
is deleted once it goes out of scope. At construction, the
<code>TempDir</code> creates a new directory with a randomly generated name.</p>
<p>The default constructor, <a href="struct.TempDir.html#method.new"><code>TempDir::new()</code></a>, creates directories in
the location returned by <a href="env/fn.temp_dir.html" title="fn tempfile::env::temp_dir"><code>env::temp_dir()</code></a>, but <code>TempDir</code>
can be configured to manage a temporary directory in any location
by constructing with a <a href="struct.Builder.html"><code>Builder</code></a>.</p>
<p>After creating a <code>TempDir</code>, work with the file system by doing
standard <a href="http://doc.rust-lang.org/std/fs/index.html"><code>std::fs</code></a> file system operations on its <a href="http://doc.rust-lang.org/std/path/struct.Path.html"><code>Path</code></a>,
which can be retrieved with <a href="struct.TempDir.html#method.path"><code>TempDir::path()</code></a>. Once the <code>TempDir</code>
value is dropped, the directory at the path will be deleted, along
with any files and directories it contains. It is your responsibility
to ensure that no further file system operations are attempted
inside the temporary directory once it has been deleted.</p>
<h2 id="resource-leaking"><a class="doc-anchor" href="#resource-leaking">§</a>Resource Leaking</h2>
<p>Various platform-specific conditions may cause <code>TempDir</code> to fail
to delete the underlying directory. Its important to ensure that
handles (like <a href="http://doc.rust-lang.org/std/fs/struct.File.html"><code>File</code></a> and <a href="http://doc.rust-lang.org/std/fs/struct.ReadDir.html"><code>ReadDir</code></a>) to files inside the
directory are dropped before the <code>TempDir</code> goes out of scope. The
<code>TempDir</code> destructor will silently ignore any errors in deleting
the directory; to instead handle errors call <a href="struct.TempDir.html#method.close"><code>TempDir::close()</code></a>.</p>
<p>Note that if the program exits before the <code>TempDir</code> destructor is
run, such as via <a href="http://doc.rust-lang.org/std/process/fn.exit.html"><code>std::process::exit()</code></a>, by segfaulting, or by
receiving a signal like <code>SIGINT</code>, then the temporary directory
will not be deleted.</p>
<h2 id="examples"><a class="doc-anchor" href="#examples">§</a>Examples</h2>
<p>Create a temporary directory with a generated name:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>std::fs::File;
<span class="kw">use </span>std::io::Write;
<span class="kw">use </span>tempfile::TempDir;
<span class="comment">// Create a directory inside of `env::temp_dir()`
</span><span class="kw">let </span>tmp_dir = TempDir::new()<span class="question-mark">?</span>;</code></pre></div>
<p>Create a temporary directory with a prefix in its name:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>std::fs::File;
<span class="kw">use </span>std::io::Write;
<span class="kw">use </span>tempfile::Builder;
<span class="comment">// Create a directory inside of `env::temp_dir()`,
// whose name will begin with 'example'.
</span><span class="kw">let </span>tmp_dir = Builder::new().prefix(<span class="string">"example"</span>).tempdir()<span class="question-mark">?</span>;</code></pre></div>
</div></details><h2 id="implementations" class="section-header">Implementations<a href="#implementations" class="anchor">§</a></h2><div id="implementations-list"><details class="toggle implementors-toggle" open><summary><section id="impl-TempDir" class="impl"><a class="src rightside" href="../src/tempfile/dir/mod.rs.html#183-463">Source</a><a href="#impl-TempDir" class="anchor">§</a><h3 class="code-header">impl <a class="struct" href="struct.TempDir.html" title="struct tempfile::TempDir">TempDir</a></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.new" class="method"><a class="src rightside" href="../src/tempfile/dir/mod.rs.html#215-217">Source</a><h4 class="code-header">pub fn <a href="#method.new" class="fn">new</a>() -&gt; <a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a>&lt;<a class="struct" href="struct.TempDir.html" title="struct tempfile::TempDir">TempDir</a>&gt;</h4></section></summary><div class="docblock"><p>Attempts to make a temporary directory inside of <code>env::temp_dir()</code>.</p>
<p>See <a href="struct.Builder.html"><code>Builder</code></a> for more configuration.</p>
<p>The directory and everything inside it will be automatically deleted
once the returned <code>TempDir</code> is destroyed.</p>
<h5 id="errors"><a class="doc-anchor" href="#errors">§</a>Errors</h5>
<p>If the directory can not be created, <code>Err</code> is returned.</p>
<h5 id="examples-1"><a class="doc-anchor" href="#examples-1">§</a>Examples</h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>std::fs::File;
<span class="kw">use </span>std::io::Write;
<span class="kw">use </span>tempfile::TempDir;
<span class="comment">// Create a directory inside of `env::temp_dir()`
</span><span class="kw">let </span>tmp_dir = TempDir::new()<span class="question-mark">?</span>;
<span class="kw">let </span>file_path = tmp_dir.path().join(<span class="string">"my-temporary-note.txt"</span>);
<span class="kw">let </span><span class="kw-2">mut </span>tmp_file = File::create(file_path)<span class="question-mark">?</span>;
<span class="macro">writeln!</span>(tmp_file, <span class="string">"Brian was here. Briefly."</span>)<span class="question-mark">?</span>;
<span class="comment">// `tmp_dir` goes out of scope, the directory as well as
// `tmp_file` will be deleted here.</span></code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.new_in" class="method"><a class="src rightside" href="../src/tempfile/dir/mod.rs.html#241-243">Source</a><h4 class="code-header">pub fn <a href="#method.new_in" class="fn">new_in</a>&lt;P: <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/convert/trait.AsRef.html" title="trait core::convert::AsRef">AsRef</a>&lt;<a class="struct" href="https://doc.rust-lang.org/1.84.1/std/path/struct.Path.html" title="struct std::path::Path">Path</a>&gt;&gt;(dir: P) -&gt; <a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a>&lt;<a class="struct" href="struct.TempDir.html" title="struct tempfile::TempDir">TempDir</a>&gt;</h4></section></summary><div class="docblock"><p>Attempts to make a temporary directory inside of <code>dir</code>.
The directory and everything inside it will be automatically
deleted once the returned <code>TempDir</code> is destroyed.</p>
<h5 id="errors-1"><a class="doc-anchor" href="#errors-1">§</a>Errors</h5>
<p>If the directory can not be created, <code>Err</code> is returned.</p>
<h5 id="examples-2"><a class="doc-anchor" href="#examples-2">§</a>Examples</h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>std::fs::{<span class="self">self</span>, File};
<span class="kw">use </span>std::io::Write;
<span class="kw">use </span>tempfile::TempDir;
<span class="comment">// Create a directory inside of the current directory
</span><span class="kw">let </span>tmp_dir = TempDir::new_in(<span class="string">"."</span>)<span class="question-mark">?</span>;
<span class="kw">let </span>file_path = tmp_dir.path().join(<span class="string">"my-temporary-note.txt"</span>);
<span class="kw">let </span><span class="kw-2">mut </span>tmp_file = File::create(file_path)<span class="question-mark">?</span>;
<span class="macro">writeln!</span>(tmp_file, <span class="string">"Brian was here. Briefly."</span>)<span class="question-mark">?</span>;</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.with_prefix" class="method"><a class="src rightside" href="../src/tempfile/dir/mod.rs.html#266-268">Source</a><h4 class="code-header">pub fn <a href="#method.with_prefix" class="fn">with_prefix</a>&lt;S: <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/convert/trait.AsRef.html" title="trait core::convert::AsRef">AsRef</a>&lt;<a class="struct" href="https://doc.rust-lang.org/1.84.1/std/ffi/os_str/struct.OsStr.html" title="struct std::ffi::os_str::OsStr">OsStr</a>&gt;&gt;(prefix: S) -&gt; <a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a>&lt;<a class="struct" href="struct.TempDir.html" title="struct tempfile::TempDir">TempDir</a>&gt;</h4></section></summary><div class="docblock"><p>Attempts to make a temporary directory with the specified prefix inside of
<code>env::temp_dir()</code>. The directory and everything inside it will be automatically
deleted once the returned <code>TempDir</code> is destroyed.</p>
<h5 id="errors-2"><a class="doc-anchor" href="#errors-2">§</a>Errors</h5>
<p>If the directory can not be created, <code>Err</code> is returned.</p>
<h5 id="examples-3"><a class="doc-anchor" href="#examples-3">§</a>Examples</h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>std::fs::{<span class="self">self</span>, File};
<span class="kw">use </span>std::io::Write;
<span class="kw">use </span>tempfile::TempDir;
<span class="comment">// Create a directory inside of the current directory
</span><span class="kw">let </span>tmp_dir = TempDir::with_prefix(<span class="string">"foo-"</span>)<span class="question-mark">?</span>;
<span class="kw">let </span>tmp_name = tmp_dir.path().file_name().unwrap().to_str().unwrap();
<span class="macro">assert!</span>(tmp_name.starts_with(<span class="string">"foo-"</span>));</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.with_suffix" class="method"><a class="src rightside" href="../src/tempfile/dir/mod.rs.html#291-293">Source</a><h4 class="code-header">pub fn <a href="#method.with_suffix" class="fn">with_suffix</a>&lt;S: <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/convert/trait.AsRef.html" title="trait core::convert::AsRef">AsRef</a>&lt;<a class="struct" href="https://doc.rust-lang.org/1.84.1/std/ffi/os_str/struct.OsStr.html" title="struct std::ffi::os_str::OsStr">OsStr</a>&gt;&gt;(suffix: S) -&gt; <a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a>&lt;<a class="struct" href="struct.TempDir.html" title="struct tempfile::TempDir">TempDir</a>&gt;</h4></section></summary><div class="docblock"><p>Attempts to make a temporary directory with the specified suffix inside of
<code>env::temp_dir()</code>. The directory and everything inside it will be automatically
deleted once the returned <code>TempDir</code> is destroyed.</p>
<h5 id="errors-3"><a class="doc-anchor" href="#errors-3">§</a>Errors</h5>
<p>If the directory can not be created, <code>Err</code> is returned.</p>
<h5 id="examples-4"><a class="doc-anchor" href="#examples-4">§</a>Examples</h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>std::fs::{<span class="self">self</span>, File};
<span class="kw">use </span>std::io::Write;
<span class="kw">use </span>tempfile::TempDir;
<span class="comment">// Create a directory inside of the current directory
</span><span class="kw">let </span>tmp_dir = TempDir::with_suffix(<span class="string">"-foo"</span>)<span class="question-mark">?</span>;
<span class="kw">let </span>tmp_name = tmp_dir.path().file_name().unwrap().to_str().unwrap();
<span class="macro">assert!</span>(tmp_name.ends_with(<span class="string">"-foo"</span>));</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.with_suffix_in" class="method"><a class="src rightside" href="../src/tempfile/dir/mod.rs.html#315-320">Source</a><h4 class="code-header">pub fn <a href="#method.with_suffix_in" class="fn">with_suffix_in</a>&lt;S: <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/convert/trait.AsRef.html" title="trait core::convert::AsRef">AsRef</a>&lt;<a class="struct" href="https://doc.rust-lang.org/1.84.1/std/ffi/os_str/struct.OsStr.html" title="struct std::ffi::os_str::OsStr">OsStr</a>&gt;, P: <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/convert/trait.AsRef.html" title="trait core::convert::AsRef">AsRef</a>&lt;<a class="struct" href="https://doc.rust-lang.org/1.84.1/std/path/struct.Path.html" title="struct std::path::Path">Path</a>&gt;&gt;(
suffix: S,
dir: P,
) -&gt; <a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a>&lt;<a class="struct" href="struct.TempDir.html" title="struct tempfile::TempDir">TempDir</a>&gt;</h4></section></summary><div class="docblock"><p>Attempts to make a temporary directory with the specified prefix inside
the specified directory. The directory and everything inside it will be
automatically deleted once the returned <code>TempDir</code> is destroyed.</p>
<h5 id="errors-4"><a class="doc-anchor" href="#errors-4">§</a>Errors</h5>
<p>If the directory can not be created, <code>Err</code> is returned.</p>
<h5 id="examples-5"><a class="doc-anchor" href="#examples-5">§</a>Examples</h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>std::fs::{<span class="self">self</span>, File};
<span class="kw">use </span>std::io::Write;
<span class="kw">use </span>tempfile::TempDir;
<span class="comment">// Create a directory inside of the current directory
</span><span class="kw">let </span>tmp_dir = TempDir::with_suffix_in(<span class="string">"-foo"</span>, <span class="string">"."</span>)<span class="question-mark">?</span>;
<span class="kw">let </span>tmp_name = tmp_dir.path().file_name().unwrap().to_str().unwrap();
<span class="macro">assert!</span>(tmp_name.ends_with(<span class="string">"-foo"</span>));</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.with_prefix_in" class="method"><a class="src rightside" href="../src/tempfile/dir/mod.rs.html#343-348">Source</a><h4 class="code-header">pub fn <a href="#method.with_prefix_in" class="fn">with_prefix_in</a>&lt;S: <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/convert/trait.AsRef.html" title="trait core::convert::AsRef">AsRef</a>&lt;<a class="struct" href="https://doc.rust-lang.org/1.84.1/std/ffi/os_str/struct.OsStr.html" title="struct std::ffi::os_str::OsStr">OsStr</a>&gt;, P: <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/convert/trait.AsRef.html" title="trait core::convert::AsRef">AsRef</a>&lt;<a class="struct" href="https://doc.rust-lang.org/1.84.1/std/path/struct.Path.html" title="struct std::path::Path">Path</a>&gt;&gt;(
prefix: S,
dir: P,
) -&gt; <a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a>&lt;<a class="struct" href="struct.TempDir.html" title="struct tempfile::TempDir">TempDir</a>&gt;</h4></section></summary><div class="docblock"><p>Attempts to make a temporary directory with the specified prefix inside
the specified directory. The directory and everything inside it will be
automatically deleted once the returned <code>TempDir</code> is destroyed.</p>
<h5 id="errors-5"><a class="doc-anchor" href="#errors-5">§</a>Errors</h5>
<p>If the directory can not be created, <code>Err</code> is returned.</p>
<h5 id="examples-6"><a class="doc-anchor" href="#examples-6">§</a>Examples</h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>std::fs::{<span class="self">self</span>, File};
<span class="kw">use </span>std::io::Write;
<span class="kw">use </span>tempfile::TempDir;
<span class="comment">// Create a directory inside of the current directory
</span><span class="kw">let </span>tmp_dir = TempDir::with_prefix_in(<span class="string">"foo-"</span>, <span class="string">"."</span>)<span class="question-mark">?</span>;
<span class="kw">let </span>tmp_name = tmp_dir.path().file_name().unwrap().to_str().unwrap();
<span class="macro">assert!</span>(tmp_name.starts_with(<span class="string">"foo-"</span>));</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.path" class="method"><a class="src rightside" href="../src/tempfile/dir/mod.rs.html#376-378">Source</a><h4 class="code-header">pub fn <a href="#method.path" class="fn">path</a>(&amp;self) -&gt; &amp;<a class="struct" href="https://doc.rust-lang.org/1.84.1/std/path/struct.Path.html" title="struct std::path::Path">Path</a></h4></section></summary><div class="docblock"><p>Accesses the <a href="http://doc.rust-lang.org/std/path/struct.Path.html"><code>Path</code></a> to the temporary directory.</p>
<h5 id="examples-7"><a class="doc-anchor" href="#examples-7">§</a>Examples</h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tempfile::TempDir;
<span class="kw">let </span>tmp_path;
{
<span class="kw">let </span>tmp_dir = TempDir::new()<span class="question-mark">?</span>;
tmp_path = tmp_dir.path().to_owned();
<span class="comment">// Check that the temp directory actually exists.
</span><span class="macro">assert!</span>(tmp_path.exists());
<span class="comment">// End of `tmp_dir` scope, directory will be deleted
</span>}
<span class="comment">// Temp directory should be deleted by now
</span><span class="macro">assert_eq!</span>(tmp_path.exists(), <span class="bool-val">false</span>);</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.into_path" class="method"><a class="src rightside" href="../src/tempfile/dir/mod.rs.html#405-412">Source</a><h4 class="code-header">pub fn <a href="#method.into_path" class="fn">into_path</a>(self) -&gt; <a class="struct" href="https://doc.rust-lang.org/1.84.1/std/path/struct.PathBuf.html" title="struct std::path::PathBuf">PathBuf</a></h4></section></summary><div class="docblock"><p>Persist the temporary directory to disk, returning the <a href="http://doc.rust-lang.org/std/path/struct.PathBuf.html"><code>PathBuf</code></a> where it is located.</p>
<p>This consumes the <a href="struct.TempDir.html"><code>TempDir</code></a> without deleting directory on the filesystem, meaning that
the directory will no longer be automatically deleted.</p>
<h5 id="examples-8"><a class="doc-anchor" href="#examples-8">§</a>Examples</h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>std::fs;
<span class="kw">use </span>tempfile::TempDir;
<span class="kw">let </span>tmp_dir = TempDir::new()<span class="question-mark">?</span>;
<span class="comment">// Persist the temporary directory to disk,
// getting the path where it is.
</span><span class="kw">let </span>tmp_path = tmp_dir.into_path();
<span class="comment">// Delete the temporary directory ourselves.
</span>fs::remove_dir_all(tmp_path)<span class="question-mark">?</span>;</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.close" class="method"><a class="src rightside" href="../src/tempfile/dir/mod.rs.html#451-462">Source</a><h4 class="code-header">pub fn <a href="#method.close" class="fn">close</a>(self) -&gt; <a class="type" href="https://doc.rust-lang.org/1.84.1/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.unit.html">()</a>&gt;</h4></section></summary><div class="docblock"><p>Closes and removes the temporary directory, returning a <code>Result</code>.</p>
<p>Although <code>TempDir</code> removes the directory on drop, in the destructor
any errors are ignored. To detect errors cleaning up the temporary
directory, call <code>close</code> instead.</p>
<h5 id="errors-6"><a class="doc-anchor" href="#errors-6">§</a>Errors</h5>
<p>This function may return a variety of <a href="http://doc.rust-lang.org/std/io/struct.Error.html"><code>std::io::Error</code></a>s that result from deleting
the files and directories contained with the temporary directory,
as well as from deleting the temporary directory itself. These errors
may be platform specific.</p>
<h5 id="examples-9"><a class="doc-anchor" href="#examples-9">§</a>Examples</h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>std::fs::File;
<span class="kw">use </span>std::io::Write;
<span class="kw">use </span>tempfile::TempDir;
<span class="comment">// Create a directory inside of `env::temp_dir()`.
</span><span class="kw">let </span>tmp_dir = TempDir::new()<span class="question-mark">?</span>;
<span class="kw">let </span>file_path = tmp_dir.path().join(<span class="string">"my-temporary-note.txt"</span>);
<span class="kw">let </span><span class="kw-2">mut </span>tmp_file = File::create(file_path)<span class="question-mark">?</span>;
<span class="macro">writeln!</span>(tmp_file, <span class="string">"Brian was here. Briefly."</span>)<span class="question-mark">?</span>;
<span class="comment">// By closing the `TempDir` explicitly we can check that it has
// been deleted successfully. If we don't close it explicitly,
// the directory will still be deleted when `tmp_dir` goes out
// of scope, but we won't know whether deleting the directory
// succeeded.
</span>drop(tmp_file);
tmp_dir.close()<span class="question-mark">?</span>;</code></pre></div>
</div></details></div></details></div><h2 id="trait-implementations" class="section-header">Trait Implementations<a href="#trait-implementations" class="anchor">§</a></h2><div id="trait-implementations-list"><details class="toggle implementors-toggle" open><summary><section id="impl-AsRef%3CPath%3E-for-TempDir" class="impl"><a class="src rightside" href="../src/tempfile/dir/mod.rs.html#465-469">Source</a><a href="#impl-AsRef%3CPath%3E-for-TempDir" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/convert/trait.AsRef.html" title="trait core::convert::AsRef">AsRef</a>&lt;<a class="struct" href="https://doc.rust-lang.org/1.84.1/std/path/struct.Path.html" title="struct std::path::Path">Path</a>&gt; for <a class="struct" href="struct.TempDir.html" title="struct tempfile::TempDir">TempDir</a></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.as_ref" class="method trait-impl"><a class="src rightside" href="../src/tempfile/dir/mod.rs.html#466-468">Source</a><a href="#method.as_ref" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.84.1/core/convert/trait.AsRef.html#tymethod.as_ref" class="fn">as_ref</a>(&amp;self) -&gt; &amp;<a class="struct" href="https://doc.rust-lang.org/1.84.1/std/path/struct.Path.html" title="struct std::path::Path">Path</a></h4></section></summary><div class='docblock'>Converts this type into a shared reference of the (usually inferred) input type.</div></details></div></details><details class="toggle implementors-toggle" open><summary><section id="impl-Debug-for-TempDir" class="impl"><a class="src rightside" href="../src/tempfile/dir/mod.rs.html#471-477">Source</a><a href="#impl-Debug-for-TempDir" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/fmt/trait.Debug.html" title="trait core::fmt::Debug">Debug</a> for <a class="struct" href="struct.TempDir.html" title="struct tempfile::TempDir">TempDir</a></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.fmt" class="method trait-impl"><a class="src rightside" href="../src/tempfile/dir/mod.rs.html#472-476">Source</a><a href="#method.fmt" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.84.1/core/fmt/trait.Debug.html#tymethod.fmt" class="fn">fmt</a>(&amp;self, f: &amp;mut <a class="struct" href="https://doc.rust-lang.org/1.84.1/core/fmt/struct.Formatter.html" title="struct core::fmt::Formatter">Formatter</a>&lt;'_&gt;) -&gt; <a class="type" href="https://doc.rust-lang.org/1.84.1/core/fmt/type.Result.html" title="type core::fmt::Result">Result</a></h4></section></summary><div class='docblock'>Formats the value using the given formatter. <a href="https://doc.rust-lang.org/1.84.1/core/fmt/trait.Debug.html#tymethod.fmt">Read more</a></div></details></div></details><details class="toggle implementors-toggle" open><summary><section id="impl-Drop-for-TempDir" class="impl"><a class="src rightside" href="../src/tempfile/dir/mod.rs.html#479-485">Source</a><a href="#impl-Drop-for-TempDir" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/ops/drop/trait.Drop.html" title="trait core::ops::drop::Drop">Drop</a> for <a class="struct" href="struct.TempDir.html" title="struct tempfile::TempDir">TempDir</a></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.drop" class="method trait-impl"><a class="src rightside" href="../src/tempfile/dir/mod.rs.html#480-484">Source</a><a href="#method.drop" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.84.1/core/ops/drop/trait.Drop.html#tymethod.drop" class="fn">drop</a>(&amp;mut self)</h4></section></summary><div class='docblock'>Executes the destructor for this type. <a href="https://doc.rust-lang.org/1.84.1/core/ops/drop/trait.Drop.html#tymethod.drop">Read more</a></div></details></div></details></div><h2 id="synthetic-implementations" class="section-header">Auto Trait Implementations<a href="#synthetic-implementations" class="anchor">§</a></h2><div id="synthetic-implementations-list"><section id="impl-Freeze-for-TempDir" class="impl"><a href="#impl-Freeze-for-TempDir" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/marker/trait.Freeze.html" title="trait core::marker::Freeze">Freeze</a> for <a class="struct" href="struct.TempDir.html" title="struct tempfile::TempDir">TempDir</a></h3></section><section id="impl-RefUnwindSafe-for-TempDir" class="impl"><a href="#impl-RefUnwindSafe-for-TempDir" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/panic/unwind_safe/trait.RefUnwindSafe.html" title="trait core::panic::unwind_safe::RefUnwindSafe">RefUnwindSafe</a> for <a class="struct" href="struct.TempDir.html" title="struct tempfile::TempDir">TempDir</a></h3></section><section id="impl-Send-for-TempDir" class="impl"><a href="#impl-Send-for-TempDir" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> for <a class="struct" href="struct.TempDir.html" title="struct tempfile::TempDir">TempDir</a></h3></section><section id="impl-Sync-for-TempDir" class="impl"><a href="#impl-Sync-for-TempDir" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> for <a class="struct" href="struct.TempDir.html" title="struct tempfile::TempDir">TempDir</a></h3></section><section id="impl-Unpin-for-TempDir" class="impl"><a href="#impl-Unpin-for-TempDir" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/marker/trait.Unpin.html" title="trait core::marker::Unpin">Unpin</a> for <a class="struct" href="struct.TempDir.html" title="struct tempfile::TempDir">TempDir</a></h3></section><section id="impl-UnwindSafe-for-TempDir" class="impl"><a href="#impl-UnwindSafe-for-TempDir" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/panic/unwind_safe/trait.UnwindSafe.html" title="trait core::panic::unwind_safe::UnwindSafe">UnwindSafe</a> for <a class="struct" href="struct.TempDir.html" title="struct tempfile::TempDir">TempDir</a></h3></section></div><h2 id="blanket-implementations" class="section-header">Blanket Implementations<a href="#blanket-implementations" class="anchor">§</a></h2><div id="blanket-implementations-list"><details class="toggle implementors-toggle"><summary><section id="impl-Any-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.84.1/src/core/any.rs.html#138">Source</a><a href="#impl-Any-for-T" class="anchor">§</a><h3 class="code-header">impl&lt;T&gt; <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/any/trait.Any.html" title="trait core::any::Any">Any</a> for T<div class="where">where
T: 'static + ?<a class="trait" href="https://doc.rust-lang.org/1.84.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.type_id" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.84.1/src/core/any.rs.html#139">Source</a><a href="#method.type_id" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.84.1/core/any/trait.Any.html#tymethod.type_id" class="fn">type_id</a>(&amp;self) -&gt; <a class="struct" href="https://doc.rust-lang.org/1.84.1/core/any/struct.TypeId.html" title="struct core::any::TypeId">TypeId</a></h4></section></summary><div class='docblock'>Gets the <code>TypeId</code> of <code>self</code>. <a href="https://doc.rust-lang.org/1.84.1/core/any/trait.Any.html#tymethod.type_id">Read more</a></div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-Borrow%3CT%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.84.1/src/core/borrow.rs.html#209">Source</a><a href="#impl-Borrow%3CT%3E-for-T" class="anchor">§</a><h3 class="code-header">impl&lt;T&gt; <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/borrow/trait.Borrow.html" title="trait core::borrow::Borrow">Borrow</a>&lt;T&gt; for T<div class="where">where
T: ?<a class="trait" href="https://doc.rust-lang.org/1.84.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.borrow" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.84.1/src/core/borrow.rs.html#211">Source</a><a href="#method.borrow" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.84.1/core/borrow/trait.Borrow.html#tymethod.borrow" class="fn">borrow</a>(&amp;self) -&gt; <a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.reference.html">&amp;T</a></h4></section></summary><div class='docblock'>Immutably borrows from an owned value. <a href="https://doc.rust-lang.org/1.84.1/core/borrow/trait.Borrow.html#tymethod.borrow">Read more</a></div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-BorrowMut%3CT%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.84.1/src/core/borrow.rs.html#217">Source</a><a href="#impl-BorrowMut%3CT%3E-for-T" class="anchor">§</a><h3 class="code-header">impl&lt;T&gt; <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/borrow/trait.BorrowMut.html" title="trait core::borrow::BorrowMut">BorrowMut</a>&lt;T&gt; for T<div class="where">where
T: ?<a class="trait" href="https://doc.rust-lang.org/1.84.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.borrow_mut" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.84.1/src/core/borrow.rs.html#218">Source</a><a href="#method.borrow_mut" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.84.1/core/borrow/trait.BorrowMut.html#tymethod.borrow_mut" class="fn">borrow_mut</a>(&amp;mut self) -&gt; <a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.reference.html">&amp;mut T</a></h4></section></summary><div class='docblock'>Mutably borrows from an owned value. <a href="https://doc.rust-lang.org/1.84.1/core/borrow/trait.BorrowMut.html#tymethod.borrow_mut">Read more</a></div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-From%3CT%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.84.1/src/core/convert/mod.rs.html#765">Source</a><a href="#impl-From%3CT%3E-for-T" class="anchor">§</a><h3 class="code-header">impl&lt;T&gt; <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/convert/trait.From.html" title="trait core::convert::From">From</a>&lt;T&gt; for T</h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.from" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.84.1/src/core/convert/mod.rs.html#768">Source</a><a href="#method.from" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.84.1/core/convert/trait.From.html#tymethod.from" class="fn">from</a>(t: T) -&gt; T</h4></section></summary><div class="docblock"><p>Returns the argument unchanged.</p>
</div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-Into%3CU%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.84.1/src/core/convert/mod.rs.html#748-750">Source</a><a href="#impl-Into%3CU%3E-for-T" class="anchor">§</a><h3 class="code-header">impl&lt;T, U&gt; <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/convert/trait.Into.html" title="trait core::convert::Into">Into</a>&lt;U&gt; for T<div class="where">where
U: <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/convert/trait.From.html" title="trait core::convert::From">From</a>&lt;T&gt;,</div></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.into" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.84.1/src/core/convert/mod.rs.html#758">Source</a><a href="#method.into" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.84.1/core/convert/trait.Into.html#tymethod.into" class="fn">into</a>(self) -&gt; U</h4></section></summary><div class="docblock"><p>Calls <code>U::from(self)</code>.</p>
<p>That is, this conversion is whatever the implementation of
<code><a href="https://doc.rust-lang.org/1.84.1/core/convert/trait.From.html" title="trait core::convert::From">From</a>&lt;T&gt; for U</code> chooses to do.</p>
</div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-TryFrom%3CU%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.84.1/src/core/convert/mod.rs.html#805-807">Source</a><a href="#impl-TryFrom%3CU%3E-for-T" class="anchor">§</a><h3 class="code-header">impl&lt;T, U&gt; <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a>&lt;U&gt; for T<div class="where">where
U: <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/convert/trait.Into.html" title="trait core::convert::Into">Into</a>&lt;T&gt;,</div></h3></section></summary><div class="impl-items"><details class="toggle" open><summary><section id="associatedtype.Error-1" class="associatedtype trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.84.1/src/core/convert/mod.rs.html#809">Source</a><a href="#associatedtype.Error-1" class="anchor">§</a><h4 class="code-header">type <a href="https://doc.rust-lang.org/1.84.1/core/convert/trait.TryFrom.html#associatedtype.Error" class="associatedtype">Error</a> = <a class="enum" href="https://doc.rust-lang.org/1.84.1/core/convert/enum.Infallible.html" title="enum core::convert::Infallible">Infallible</a></h4></section></summary><div class='docblock'>The type returned in the event of a conversion error.</div></details><details class="toggle method-toggle" open><summary><section id="method.try_from" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.84.1/src/core/convert/mod.rs.html#812">Source</a><a href="#method.try_from" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.84.1/core/convert/trait.TryFrom.html#tymethod.try_from" class="fn">try_from</a>(value: U) -&gt; <a class="enum" href="https://doc.rust-lang.org/1.84.1/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;T, &lt;T as <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a>&lt;U&gt;&gt;::<a class="associatedtype" href="https://doc.rust-lang.org/1.84.1/core/convert/trait.TryFrom.html#associatedtype.Error" title="type core::convert::TryFrom::Error">Error</a>&gt;</h4></section></summary><div class='docblock'>Performs the conversion.</div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-TryInto%3CU%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.84.1/src/core/convert/mod.rs.html#790-792">Source</a><a href="#impl-TryInto%3CU%3E-for-T" class="anchor">§</a><h3 class="code-header">impl&lt;T, U&gt; <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/convert/trait.TryInto.html" title="trait core::convert::TryInto">TryInto</a>&lt;U&gt; for T<div class="where">where
U: <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a>&lt;T&gt;,</div></h3></section></summary><div class="impl-items"><details class="toggle" open><summary><section id="associatedtype.Error" class="associatedtype trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.84.1/src/core/convert/mod.rs.html#794">Source</a><a href="#associatedtype.Error" class="anchor">§</a><h4 class="code-header">type <a href="https://doc.rust-lang.org/1.84.1/core/convert/trait.TryInto.html#associatedtype.Error" class="associatedtype">Error</a> = &lt;U as <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a>&lt;T&gt;&gt;::<a class="associatedtype" href="https://doc.rust-lang.org/1.84.1/core/convert/trait.TryFrom.html#associatedtype.Error" title="type core::convert::TryFrom::Error">Error</a></h4></section></summary><div class='docblock'>The type returned in the event of a conversion error.</div></details><details class="toggle method-toggle" open><summary><section id="method.try_into" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.84.1/src/core/convert/mod.rs.html#797">Source</a><a href="#method.try_into" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.84.1/core/convert/trait.TryInto.html#tymethod.try_into" class="fn">try_into</a>(self) -&gt; <a class="enum" href="https://doc.rust-lang.org/1.84.1/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;U, &lt;U as <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a>&lt;T&gt;&gt;::<a class="associatedtype" href="https://doc.rust-lang.org/1.84.1/core/convert/trait.TryFrom.html#associatedtype.Error" title="type core::convert::TryFrom::Error">Error</a>&gt;</h4></section></summary><div class='docblock'>Performs the conversion.</div></details></div></details></div></section></div></main></body></html>