<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Eagle</title>
    <description>The latest articles on DEV Community by Eagle (@eaglebk).</description>
    <link>https://dev.to/eaglebk</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F345905%2Fb82c94a3-56d3-4a51-8e11-22911a6d5711.jpeg</url>
      <title>DEV Community: Eagle</title>
      <link>https://dev.to/eaglebk</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/eaglebk"/>
    <language>en</language>
    <item>
      <title>Rust Article Roadmap: From Ownership and Traits to Files and Tokio</title>
      <dc:creator>Eagle</dc:creator>
      <pubDate>Thu, 03 Dec 2026 13:20:09 +0000</pubDate>
      <link>https://dev.to/eaglebk/rust-article-roadmap-from-ownership-and-traits-to-files-and-tokio-31k4</link>
      <guid>https://dev.to/eaglebk/rust-article-roadmap-from-ownership-and-traits-to-files-and-tokio-31k4</guid>
      <description>&lt;p&gt;Once the Rust posts here passed a couple dozen entries, reading them in reverse chronological order stopped making sense. I put together this map to group the English guides in a practical learning order — from core syntax and memory rules to traits, file I/O, and performance.&lt;/p&gt;

&lt;p&gt;Most posts include runnable Rust Playground slides and a short quiz at the end.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Core Types and Control Flow
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://eagle2000.space/en/rust/rust-conditionals/" rel="noopener noreferrer"&gt;Conditionals and Branching in Rust&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://eagle2000.space/en/rust/rust-enums/" rel="noopener noreferrer"&gt;Enums in Rust: From Syntax to Pattern Matching&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://eagle2000.space/en/rust/rust-pattern-matching-deep-dive/" rel="noopener noreferrer"&gt;Pattern Matching Deep Dive: Destructuring and Guards&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. Memory, Lifetimes, and Smart Pointers
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://eagle2000.space/en/rust/rust-raii-box-deref/" rel="noopener noreferrer"&gt;Memory Management in Rust: RAII, &lt;code&gt;Box&amp;lt;T&amp;gt;&lt;/code&gt;, and Deref Coercion&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://eagle2000.space/en/rust/rust-lifetimes-deep-dive/" rel="noopener noreferrer"&gt;Lifetimes Deep Dive: Elision, HRTB, and Variance&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://eagle2000.space/en/rust/rust-interior-mutability/" rel="noopener noreferrer"&gt;Interior Mutability: &lt;code&gt;Cell&lt;/code&gt; and &lt;code&gt;RefCell&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3. Traits, Closures, and Collections
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://eagle2000.space/en/rust/rust-traits/" rel="noopener noreferrer"&gt;Traits and Static Polymorphism&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://eagle2000.space/en/rust/rust-dyn-traits/" rel="noopener noreferrer"&gt;Trait Objects (&lt;code&gt;dyn Trait&lt;/code&gt;) and Dynamic Polymorphism&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://eagle2000.space/en/rust/rust-advanced-traits/" rel="noopener noreferrer"&gt;Advanced Traits: Associated Types and Supertraits&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://eagle2000.space/en/rust/rust-closures/" rel="noopener noreferrer"&gt;Closures in Rust: Functions with Captured State&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://eagle2000.space/en/rust/rust-iterators-deep-dive/" rel="noopener noreferrer"&gt;Iterators Deep Dive: Lazy Processing Without Extra Allocations&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://eagle2000.space/en/rust/rust-collections-overview/" rel="noopener noreferrer"&gt;Choosing &lt;code&gt;std::collections&lt;/code&gt;: &lt;code&gt;BTreeMap&lt;/code&gt;, &lt;code&gt;VecDeque&lt;/code&gt;, &lt;code&gt;BinaryHeap&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  4. Files, I/O, and Performance
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://eagle2000.space/en/rust/rust-files-tempfile-and-docs/" rel="noopener noreferrer"&gt;File Modes, &lt;code&gt;File::options()&lt;/code&gt;, and &lt;code&gt;Path&lt;/code&gt; / &lt;code&gt;PathBuf&lt;/code&gt; in Rust&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://eagle2000.space/en/rust/rust-bufreader/" rel="noopener noreferrer"&gt;BufReader in Rust: Speeding Up File I/O&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://eagle2000.space/en/rust/rust-io-bufread-and-errors/" rel="noopener noreferrer"&gt;Fast I/O in Rust: Buffering, Tests, and Error Handling&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://eagle2000.space/en/rust/rust-simd-and-gta/" rel="noopener noreferrer"&gt;SIMD in Practice (and What GTA Has to Do with It)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://eagle2000.space/en/rust/http-202-async-email-timeout/" rel="noopener noreferrer"&gt;Production Pitfall: &lt;code&gt;202 Accepted&lt;/code&gt; and a 9-Second Client Timeout&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Crates of the Day: &lt;a href="https://eagle2000.space/en/rust/crate-of-the-day-rayon/" rel="noopener noreferrer"&gt;Rayon (Data Parallelism in One Line)&lt;/a&gt; and &lt;a href="https://eagle2000.space/en/rust/crate-of-the-day-strum/" rel="noopener noreferrer"&gt;Strum (Enum Iteration and Conversion)&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://eagle2000.space/en/rust/rust-roadmap/" rel="noopener noreferrer"&gt;eagle2000.space&lt;/a&gt; with runnable Rust Playground slides and an interactive quiz.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>rust</category>
      <category>roadmap</category>
      <category>ownership</category>
      <category>traits</category>
    </item>
    <item>
      <title>File Modes, Builder Pattern, and Path / PathBuf in Rust</title>
      <dc:creator>Eagle</dc:creator>
      <pubDate>Thu, 03 Dec 2026 13:20:04 +0000</pubDate>
      <link>https://dev.to/eaglebk/file-modes-builder-pattern-and-path-pathbuf-in-rust-2f68</link>
      <guid>https://dev.to/eaglebk/file-modes-builder-pattern-and-path-pathbuf-in-rust-2f68</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Almost every CLI tool, web service, or database eventually hits disk I/O. In Rust, the filesystem API is designed to avoid redundant allocations and prevent accidental descriptor bugs.&lt;/p&gt;

&lt;p&gt;In this guide, I'll walk through two core topics for filesystem work:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Configuring OS file descriptors with the &lt;strong&gt;Builder&lt;/strong&gt; pattern in &lt;code&gt;File::options()&lt;/code&gt; and how those methods map to POSIX flags.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;&lt;code&gt;Path&lt;/code&gt; vs &lt;code&gt;PathBuf&lt;/code&gt;&lt;/strong&gt; pair, unsized types (&lt;code&gt;DST&lt;/code&gt;), and designing ergonomic public APIs with &lt;strong&gt;&lt;code&gt;impl AsRef&amp;lt;Path&amp;gt;&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  1. File Descriptors and the Builder Pattern in &lt;code&gt;File::options()&lt;/code&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What Is a File Descriptor?
&lt;/h3&gt;

&lt;p&gt;When a program opens a file, network socket, or pipe, the operating system does not hand over raw disk access. Instead, it creates an entry in the process's open-file table and returns a small integer (like &lt;code&gt;3&lt;/code&gt; or &lt;code&gt;4&lt;/code&gt;) called a &lt;strong&gt;file descriptor&lt;/strong&gt; (&lt;code&gt;fd&lt;/code&gt;). Every process starts with three standard descriptors already open: &lt;code&gt;0&lt;/code&gt; for &lt;code&gt;stdin&lt;/code&gt;, &lt;code&gt;1&lt;/code&gt; for &lt;code&gt;stdout&lt;/code&gt;, and &lt;code&gt;2&lt;/code&gt; for &lt;code&gt;stderr&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;On Windows, the equivalent kernel identifier is a &lt;code&gt;HANDLE&lt;/code&gt;. Rust's &lt;code&gt;std::fs::File&lt;/code&gt; wraps this OS handle and automatically closes it when the &lt;code&gt;File&lt;/code&gt; goes out of scope (via &lt;code&gt;Drop&lt;/code&gt;), preventing descriptor leaks.&lt;/p&gt;

&lt;h4&gt;
  
  
  How to Inspect File Descriptors in the Terminal
&lt;/h4&gt;

&lt;p&gt;{{&amp;lt; spoiler title="Linux and macOS: listing open file descriptors" &amp;gt;}}&lt;br&gt;
On Linux, a process's open descriptors appear as symlinks inside &lt;code&gt;/proc/&amp;lt;PID&amp;gt;/fd&lt;/code&gt;. You can bind a test file to descriptor &lt;code&gt;3&lt;/code&gt; directly in your shell and inspect the table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# 1. Attach test.log to file descriptor 3&lt;/span&gt;
&lt;span class="nb"&gt;exec&lt;/span&gt; 3&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; test.log

&lt;span class="c"&gt;# 2. List open descriptors for the current shell ($$ is its PID)&lt;/span&gt;
ls -la /proc/&lt;span class="nv"&gt;$$&lt;/span&gt;/fd
&lt;span class="c"&gt;# Linux output: 0 -&amp;gt; /dev/pts/0, 1 -&amp;gt; /dev/pts/0, 2 -&amp;gt; /dev/pts/0, 3 -&amp;gt; /tmp/test.log&lt;/span&gt;

&lt;span class="c"&gt;# On macOS (no /proc), use lsof instead:&lt;/span&gt;
lsof -p &lt;span class="nv"&gt;$$&lt;/span&gt;

&lt;span class="c"&gt;# 3. Close descriptor 3 and remove the test file&lt;/span&gt;
&lt;span class="nb"&gt;exec&lt;/span&gt; 3&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt;- &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; rm test.log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;{{&amp;lt; /spoiler &amp;gt;}}&lt;/p&gt;

&lt;p&gt;{{&amp;lt; spoiler title="Windows: inspecting open HANDLEs" &amp;gt;}}&lt;br&gt;
Windows does not expose &lt;code&gt;/proc&lt;/code&gt;, so you can inspect open &lt;code&gt;HANDLE&lt;/code&gt;s in two ways:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Built-in Resource Monitor:&lt;/strong&gt; run &lt;code&gt;resmon.exe&lt;/code&gt; from PowerShell or &lt;code&gt;Win + R&lt;/code&gt;, open the &lt;strong&gt;CPU&lt;/strong&gt; tab, and check &lt;strong&gt;Associated Handles&lt;/strong&gt; to see every file handle held by a process.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;In PowerShell / CMD&lt;/strong&gt; using Microsoft Sysinternals &lt;code&gt;handle.exe&lt;/code&gt;:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;   &lt;span class="c1"&gt;# Show all open handles for notepad.exe&lt;/span&gt;
   &lt;span class="n"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exe&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="n"&gt;notepad&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exe&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;{{&amp;lt; /spoiler &amp;gt;}}&lt;/p&gt;
&lt;h3&gt;
  
  
  Mapping to POSIX System Calls
&lt;/h3&gt;

&lt;p&gt;At the OS level, Unix/POSIX and Windows require access bitmasks when opening a file via &lt;code&gt;open(2)&lt;/code&gt; or &lt;code&gt;CreateFileW&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;{{&amp;lt; alert "comment" &amp;gt;}}&lt;br&gt;
&lt;strong&gt;What is POSIX?&lt;/strong&gt; It is a family of standards for Unix-like operating systems (such as Linux, macOS, and FreeBSD) that defines common system calls and constants for files, processes, and sockets. Windows uses its own native API (&lt;code&gt;CreateFileW&lt;/code&gt;), and Rust's standard library automatically translates &lt;code&gt;File::options()&lt;/code&gt; into the right OS-specific flags for each platform.&lt;br&gt;
{{&amp;lt; /alert &amp;gt;}}&lt;/p&gt;

&lt;p&gt;In POSIX, these flags are represented as constants:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;O_RDONLY&lt;/code&gt; - open for reading only.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;O_WRONLY | O_CREAT&lt;/code&gt; - open for writing, creating the file if it does not exist.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;O_APPEND&lt;/code&gt; - append all writes strictly to the end of the file.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;O_EXCL&lt;/code&gt; - fail if the file already exists (atomic creation guarantee).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;O_TRUNC&lt;/code&gt; - truncate the file size to 0 upon opening.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Rust's &lt;code&gt;File::open(path)&lt;/code&gt; and &lt;code&gt;File::create(path)&lt;/code&gt; provide common presets for these flags. Whenever you need a custom combination (for example, opening a file for both reading and appending without truncating), use &lt;code&gt;std::fs::OpenOptions&lt;/code&gt; or &lt;code&gt;File::options()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Quick cheat sheet:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Read-only:&lt;/strong&gt; &lt;code&gt;File::open(path)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Create or truncate to zero (&lt;code&gt;O_TRUNC&lt;/code&gt;):&lt;/strong&gt; &lt;code&gt;File::create(path)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Create if missing and append:&lt;/strong&gt; &lt;code&gt;File::options().create(true).append(true).open(path)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Applying the Builder Pattern
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;File::options()&lt;/code&gt; returns a configuration builder using the &lt;strong&gt;Builder pattern&lt;/strong&gt; (covered in detail in &lt;a href="https://eagle2000.space/en/rust/rust-builder/" rel="noopener noreferrer"&gt;The Builder Pattern in Rust&lt;/a&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;fs&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nc"&gt;File&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="kd"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;File&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;options&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;        &lt;span class="c1"&gt;// Enables read mode&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;       &lt;span class="c1"&gt;// Combined with .read(true), maps to O_RDWR&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;      &lt;span class="c1"&gt;// Creates the file if missing (O_CREAT)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;      &lt;span class="c1"&gt;// Moves the write cursor to the end (O_APPEND)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"app.log"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each method updates the builder's internal state and returns &lt;code&gt;&amp;amp;mut Self&lt;/code&gt;, while the final &lt;code&gt;.open()&lt;/code&gt; call executes the OS syscall.&lt;/p&gt;

&lt;p&gt;{{&amp;lt; alert "comment" &amp;gt;}}&lt;br&gt;
&lt;strong&gt;How do you create a file without overwriting an existing one?&lt;/strong&gt; If you first check &lt;code&gt;if !path.exists()&lt;/code&gt; and then call &lt;code&gt;File::create()&lt;/code&gt;, another process can create a file with the same name in between those two steps, and your code will silently overwrite it (a TOCTOU race condition: Time-of-Check to Time-of-Use). Use &lt;code&gt;.create_new(true)&lt;/code&gt; instead: the OS performs the existence check and file creation as a single atomic step, returning &lt;code&gt;AlreadyExists&lt;/code&gt; if the file is already there (which maps to &lt;code&gt;O_CREAT | O_EXCL&lt;/code&gt; on POSIX).&lt;br&gt;
{{&amp;lt; /alert &amp;gt;}}&lt;/p&gt;
&lt;h3&gt;
  
  
  Common Pitfalls with Descriptors and &lt;code&gt;File::options()&lt;/code&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Writing to a read-only file (&lt;code&gt;os error 9&lt;/code&gt;):&lt;/strong&gt; if you open a file with &lt;code&gt;File::open("app.log")&lt;/code&gt; and try to &lt;code&gt;writeln!&lt;/code&gt; to it, Unix/Linux fails with &lt;code&gt;Bad file descriptor (os error 9)&lt;/code&gt; (on Windows, &lt;code&gt;os error 5&lt;/code&gt;, Access Denied) because that descriptor lacks write permissions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Calling &lt;code&gt;.create(true)&lt;/code&gt; without &lt;code&gt;.append&lt;/code&gt; or &lt;code&gt;.truncate&lt;/code&gt; (&lt;code&gt;os error 22&lt;/code&gt;):&lt;/strong&gt; &lt;code&gt;File::options().create(true).open("app.log")&lt;/code&gt; compiles fine, but returns &lt;code&gt;InvalidInput&lt;/code&gt; ("Invalid argument") at runtime. The OS needs to know whether to append (&lt;code&gt;.append(true)&lt;/code&gt;) or truncate (&lt;code&gt;.truncate(true)&lt;/code&gt;/&lt;code&gt;false&lt;/code&gt;). Running &lt;code&gt;cargo clippy&lt;/code&gt; catches this immediately with &lt;em&gt;file opened with &lt;code&gt;create&lt;/code&gt;, but &lt;code&gt;truncate&lt;/code&gt; behavior not defined&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Simple one-liners:&lt;/strong&gt; when you do not need custom flags or a long-lived handle, &lt;code&gt;std::fs::read_to_string(path)&lt;/code&gt; and &lt;code&gt;std::fs::write(path, data)&lt;/code&gt; are much shorter.&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  2. Path Types Anatomy: &lt;code&gt;Path&lt;/code&gt; vs &lt;code&gt;PathBuf&lt;/code&gt; and &lt;code&gt;impl AsRef&amp;lt;Path&amp;gt;&lt;/code&gt;
&lt;/h2&gt;
&lt;h3&gt;
  
  
  A Strict Analogy with String Types
&lt;/h3&gt;

&lt;p&gt;Filesystem paths in Rust follow the exact same design as string types (&lt;code&gt;&amp;amp;str&lt;/code&gt; and &lt;code&gt;String&lt;/code&gt;):&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Concept&lt;/th&gt;
&lt;th&gt;Borrowed Unsized Slice&lt;/th&gt;
&lt;th&gt;Owned Heap Buffer&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Text Data&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;&amp;amp;str&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;String&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;OS Native Strings&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;&amp;amp;OsStr&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;OsString&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Filesystem Paths&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;&amp;amp;Path&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;PathBuf&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;h3&gt;
  
  
  Why Is &lt;code&gt;Path&lt;/code&gt; an Unsized Type (DST)?
&lt;/h3&gt;

&lt;p&gt;Most Rust types have a fixed size known at compile time (a &lt;code&gt;u64&lt;/code&gt; is always 8 bytes). &lt;strong&gt;DSTs (Dynamically Sized Types)&lt;/strong&gt;, such as &lt;code&gt;str&lt;/code&gt;, &lt;code&gt;[T]&lt;/code&gt;, and &lt;code&gt;Path&lt;/code&gt; (a thin wrapper around &lt;code&gt;OsStr&lt;/code&gt;), do not: the byte length of a path like &lt;code&gt;"/var/log/app.log"&lt;/code&gt; is only known at runtime.&lt;/p&gt;

&lt;p&gt;Because the compiler cannot reserve an unknown number of bytes on the stack, you can never create a bare &lt;code&gt;let p: Path;&lt;/code&gt; variable. Instead, &lt;code&gt;Path&lt;/code&gt; always lives behind a reference or smart pointer like &lt;code&gt;&amp;amp;Path&lt;/code&gt; or &lt;code&gt;Box&amp;lt;Path&amp;gt;&lt;/code&gt;. Under the hood, &lt;code&gt;&amp;amp;Path&lt;/code&gt; is a fat pointer that carries both the memory address and the byte length of the path.&lt;/p&gt;

&lt;p&gt;In contrast, &lt;strong&gt;&lt;code&gt;PathBuf&lt;/code&gt;&lt;/strong&gt; owns a growable byte buffer on the heap and lets you mutate paths safely at runtime:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nc"&gt;PathBuf&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="kd"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;PathBuf&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/var/log"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"eagleblog"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;       &lt;span class="c1"&gt;// Inserts the OS separator ('/' on Unix, '\' on Windows)&lt;/span&gt;
&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"telemetry.log"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set_extension&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"json"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// Changes extension to /var/log/eagleblog/telemetry.json&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Ergonomic APIs with &lt;code&gt;impl AsRef&amp;lt;Path&amp;gt;&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;When designing a library or service module, taking concrete types like &lt;code&gt;fn save_data(path: &amp;amp;PathBuf)&lt;/code&gt; or &lt;code&gt;fn save_data(path: &amp;amp;str)&lt;/code&gt; is a common API mistake: callers either have to allocate a &lt;code&gt;PathBuf::from(...)&lt;/code&gt; unnecessarily, or cannot pass an existing &lt;code&gt;PathBuf&lt;/code&gt; directly.&lt;/p&gt;

&lt;p&gt;The idiomatic solution is accepting &lt;strong&gt;&lt;code&gt;impl AsRef&amp;lt;Path&amp;gt;&lt;/code&gt;&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;{{&amp;lt; code-before-after before_label="Rigid API (&amp;amp;PathBuf)" after_label="Ergonomic API (impl AsRef)" badge="0 extra allocs" caption="Comparison: impl AsRef accepts &amp;amp;str, String, &amp;amp;Path, and PathBuf with zero extra allocations at the call site" &amp;gt;}}&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nc"&gt;PathBuf&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;read_file_content&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nc"&gt;PathBuf&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;io&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;fs&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;read_to_string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Callers are forced to allocate a PathBuf:&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;read_file_content&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nc"&gt;PathBuf&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"config.toml"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;===&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;read_file_content&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="nc"&gt;AsRef&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;io&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;fs&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;read_to_string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;as_ref&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Works with any path-like type without conversions:&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;read_file_content&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"config.toml"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;read_file_content&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nc"&gt;PathBuf&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"config.toml"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;===&lt;br&gt;
Signature ;; path: &amp;amp;PathBuf ;; path: impl AsRef ;; Instead of locking callers to a heap buffer, accept any type that cheaply converts to &amp;amp;Path.&lt;br&gt;
String literal call ;; &amp;amp;PathBuf::from("config.toml") ;; "config.toml" ;; A string literal &amp;amp;str implements AsRef directly, avoiding a heap allocation.&lt;br&gt;
{{&amp;lt; /code-before-after &amp;gt;}}&lt;/p&gt;

&lt;h3&gt;
  
  
  Three Practical Tips for &lt;code&gt;impl AsRef&amp;lt;Path&amp;gt;&lt;/code&gt; and &lt;code&gt;Path&lt;/code&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Watch out for moves:&lt;/strong&gt; because &lt;code&gt;path: impl AsRef&amp;lt;Path&amp;gt;&lt;/code&gt; is passed by value, calling &lt;code&gt;fs::exists(path)&lt;/code&gt; moves &lt;code&gt;path&lt;/code&gt;, so a subsequent &lt;code&gt;fs::read_to_string(path)&lt;/code&gt; will not compile. Pass &lt;code&gt;&amp;amp;path&lt;/code&gt; instead, or bind &lt;code&gt;let path = path.as_ref();&lt;/code&gt; at the top of the function.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Storing in structs and printing:&lt;/strong&gt; to store the path inside a struct, convert it into an owned &lt;code&gt;PathBuf&lt;/code&gt; with &lt;code&gt;PathBuf::from(path.as_ref())&lt;/code&gt; (or &lt;code&gt;.to_path_buf()&lt;/code&gt;). When printing a &lt;code&gt;&amp;amp;Path&lt;/code&gt;, call &lt;code&gt;.display()&lt;/code&gt; because &lt;code&gt;Path&lt;/code&gt; does not implement &lt;code&gt;Display&lt;/code&gt; (OS paths are not guaranteed to be valid UTF-8).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Prefer &lt;code&gt;std::fs::exists&lt;/code&gt; (Rust 1.81+):&lt;/strong&gt; &lt;code&gt;path.exists()&lt;/code&gt; silently returns &lt;code&gt;false&lt;/code&gt; on permission errors, whereas &lt;code&gt;std::fs::exists(&amp;amp;path)?&lt;/code&gt; returns &lt;code&gt;io::Result&amp;lt;bool&amp;gt;&lt;/code&gt; and surfaces real I/O failures.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Walk through the interactive slides below to see &lt;code&gt;File::options()&lt;/code&gt; and &lt;code&gt;impl AsRef&amp;lt;Path&amp;gt;&lt;/code&gt; in action:&lt;/p&gt;

&lt;h3&gt;
  
  
  💻 Builder in File::options(), Path, PathBuf, and AsRef

&lt;/h3&gt;
&lt;h4&gt;
  
  
  Step 1
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Step 1: Configuring file descriptors via File::options()&lt;/span&gt;

&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;fs&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nc"&gt;File&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;io&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nc"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="n"&gt;anyhow&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;append_telemetry_log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file_path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Calling File::options() is an application of the Builder pattern for file flags:&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="kd"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;log_file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;File&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;options&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Create the file if it does not exist yet&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Append bytes strictly to the end without overwriting&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file_path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nf"&gt;writeln!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;log_file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"[LOG]: {entry}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kc"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;append_telemetry_log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"system_events.log"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Auth service started successfully"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kc"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Entry successfully appended to the log file"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="kc"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"File access error: {err}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;1. The Builder pattern in File::options()&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I covered the Builder pattern in detail in &lt;a href="https://eagle2000.space/en/rust/rust-builder/" rel="noopener noreferrer"&gt;The Builder Pattern in Rust&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;The standard library uses this pattern in &lt;code&gt;File::options()&lt;/code&gt; to clearly configure OS file-opening modes (&lt;code&gt;read&lt;/code&gt;, &lt;code&gt;write&lt;/code&gt;, &lt;code&gt;append&lt;/code&gt;, &lt;code&gt;create_new&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  Step 2
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Step 2: Flexible path types with impl AsRef&amp;lt;Path&amp;gt; and Path vs PathBuf&lt;/span&gt;

&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;PathBuf&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Using `impl AsRef&amp;lt;Path&amp;gt;` lets the function accept &amp;amp;str, String, Path, and PathBuf without explicit cloning:&lt;/span&gt;
&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;inspect_file_size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="nc"&gt;AsRef&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;io&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;u64&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;path_ref&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;as_ref&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Borrow an immutable path slice (unsized Path)&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;metadata&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fs&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;metadata&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path_ref&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kc"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;metadata&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// PathBuf is an owned, mutable path on the heap (analogous to String).&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="kd"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;owned_path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;PathBuf&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;owned_path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"config.json"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fs&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;owned_path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;r#"{"env": "prod"}"#&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// &amp;amp;str is a standard string slice.&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;str_path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"system_events.log"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fs&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;str_path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"service started\n"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Both variables of different types work with the same function via impl AsRef&amp;lt;Path&amp;gt;:&lt;/span&gt;
    &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="nf"&gt;inspect_file_size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;owned_path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kc"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Size of {:?} (PathBuf): {size} bytes"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;owned_path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="kc"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Read error {:?}: {err}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;owned_path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="nf"&gt;inspect_file_size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;str_path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kc"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Size of {:?} (&amp;amp;str): {size} bytes"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;str_path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="kc"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Read error {:?}: {err}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;str_path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;2. Path, PathBuf, and the &lt;code&gt;impl AsRef&amp;lt;Path&amp;gt;&lt;/code&gt; trait&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Path&lt;/code&gt; is a borrowed dynamically sized path slice (DST, analogous to &lt;code&gt;&amp;amp;str&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;PathBuf&lt;/code&gt; is an owned, mutable path on the heap (analogous to &lt;code&gt;String&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;impl AsRef&amp;lt;Path&amp;gt;&lt;/code&gt; lets your API accept &lt;code&gt;&amp;amp;str&lt;/code&gt;, &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;&amp;amp;Path&lt;/code&gt;, and &lt;code&gt;PathBuf&lt;/code&gt; without extra conversions.&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  Further Reading
&lt;/h2&gt;

&lt;p&gt;Most of these patterns come from John Arundel's book “The Secrets of Rust: Tools”. Chapter 5, “Files” (pages 89–114), walks through file descriptors, &lt;code&gt;File::options()&lt;/code&gt;, &lt;code&gt;Path&lt;/code&gt;, &lt;code&gt;PathBuf&lt;/code&gt;, and &lt;code&gt;AsRef&amp;lt;Path&amp;gt;&lt;/code&gt; using the &lt;code&gt;logbook&lt;/code&gt; CLI project.&lt;/p&gt;


&lt;h2&gt;
  
  
  Test Your Knowledge!
&lt;/h2&gt;

&lt;p&gt;Take a quick quiz on file modes, &lt;code&gt;Path&lt;/code&gt; / &lt;code&gt;PathBuf&lt;/code&gt;, and &lt;code&gt;AsRef&amp;lt;Path&amp;gt;&lt;/code&gt;:&lt;/p&gt;
&lt;h3&gt;
  
  
  📝 File Modes, Path, and PathBuf
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1. What is the main advantage of &lt;code&gt;File::options()&lt;/code&gt; when opening files?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;[ ] It automatically compresses files with gzip&lt;/li&gt;
&lt;li&gt;[ ] It applies the Builder pattern, offering clear, chainable methods to configure file descriptor flags (&lt;code&gt;.create(true).append(true)&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;[ ] It replaces OS system calls with an in-memory emulator&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. What is the difference between &lt;code&gt;Path&lt;/code&gt; and &lt;code&gt;PathBuf&lt;/code&gt;?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;[ ] &lt;code&gt;Path&lt;/code&gt; is only for files, while &lt;code&gt;PathBuf&lt;/code&gt; is only for directories&lt;/li&gt;
&lt;li&gt;[ ] &lt;code&gt;Path&lt;/code&gt; is only available in test builds&lt;/li&gt;
&lt;li&gt;[ ] &lt;code&gt;Path&lt;/code&gt; is an unsized, borrowed path slice (like &lt;code&gt;&amp;amp;str&lt;/code&gt;), while &lt;code&gt;PathBuf&lt;/code&gt; is an owned, heap-allocated path (like &lt;code&gt;String&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3. Why should function parameters accept &lt;code&gt;impl AsRef&amp;lt;Path&amp;gt;&lt;/code&gt;?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;[ ] It lets callers pass &lt;code&gt;&amp;amp;str&lt;/code&gt;, &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;&amp;amp;Path&lt;/code&gt;, or &lt;code&gt;PathBuf&lt;/code&gt; without manual conversions&lt;/li&gt;
&lt;li&gt;[ ] It speeds up reading files from NVMe SSDs&lt;/li&gt;
&lt;li&gt;[ ] It prevents permission-denied errors at compile time&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;4. Which &lt;code&gt;File::options()&lt;/code&gt; method atomically creates a new file and fails with &lt;code&gt;AlreadyExists&lt;/code&gt; if it already exists?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;[ ] &lt;code&gt;.truncate(true)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;[ ] &lt;code&gt;.append(true)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;[ ] &lt;code&gt;.create_new(true)&lt;/code&gt; (maps to &lt;code&gt;O_CREAT | O_EXCL&lt;/code&gt; on POSIX)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;5. What does &lt;code&gt;.append(true)&lt;/code&gt; (&lt;code&gt;O_APPEND&lt;/code&gt; on POSIX) do when opening a file?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;[ ] Overwrites the file from the beginning, erasing existing data&lt;/li&gt;
&lt;li&gt;[ ] Opens the file in read-only mode&lt;/li&gt;
&lt;li&gt;[ ] Appends all writes strictly to the end of the file without erasing existing content&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;6. What happens when you open an existing non-empty file with &lt;code&gt;.truncate(true)&lt;/code&gt; (&lt;code&gt;O_TRUNC&lt;/code&gt;)?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;[ ] Its size is reset to 0 bytes upon opening&lt;/li&gt;
&lt;li&gt;[ ] The file is copied into the system temp directory&lt;/li&gt;
&lt;li&gt;[ ] Writes are appended to the end of the file&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;7. Why can't you declare a bare &lt;code&gt;let p: Path;&lt;/code&gt; variable directly on the stack in Rust?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;[ ] &lt;code&gt;Path&lt;/code&gt; requires administrator privileges to allocate&lt;/li&gt;
&lt;li&gt;[ ] &lt;code&gt;Path&lt;/code&gt; is a DST (Dynamically Sized Type), so the compiler does not know how many bytes to reserve on the stack&lt;/li&gt;
&lt;li&gt;[ ] &lt;code&gt;Path&lt;/code&gt; can only exist inside &lt;code&gt;async&lt;/code&gt; functions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;8. What does &lt;code&gt;&amp;amp;Path&lt;/code&gt; store under the hood?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;[ ] Only a 64-bit memory address&lt;/li&gt;
&lt;li&gt;[ ] Only the byte length of the path&lt;/li&gt;
&lt;li&gt;[ ] A fat pointer containing both the memory address and the byte length&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;9. What is the TOCTOU (Time-of-Check to Time-of-Use) bug in &lt;code&gt;if !path.exists() { File::create(path)?; }&lt;/code&gt;?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;[ ] Between &lt;code&gt;exists()&lt;/code&gt; and &lt;code&gt;File::create()&lt;/code&gt;, another process can create the file and your code will overwrite it&lt;/li&gt;
&lt;li&gt;[ ] &lt;code&gt;exists()&lt;/code&gt; always returns &lt;code&gt;false&lt;/code&gt; on Windows&lt;/li&gt;
&lt;li&gt;[ ] &lt;code&gt;File::create()&lt;/code&gt; cannot create files in the working directory&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;10. What does &lt;code&gt;path.push("app.log")&lt;/code&gt; do on a &lt;code&gt;PathBuf&lt;/code&gt;?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;[ ] Creates an empty &lt;code&gt;app.log&lt;/code&gt; file on disk&lt;/li&gt;
&lt;li&gt;[ ] Appends a path segment using the current OS separator (&lt;code&gt;/&lt;/code&gt; or &lt;code&gt;\&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;[ ] Removes the last path component&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;11. Does calling &lt;code&gt;path.set_extension("json")&lt;/code&gt; on a &lt;code&gt;PathBuf&lt;/code&gt; rename the actual file on disk?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;[ ] Yes, it immediately renames the file on the filesystem&lt;/li&gt;
&lt;li&gt;[ ] No, it only updates the in-memory path inside &lt;code&gt;PathBuf&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;[ ] Yes, if the file was opened with &lt;code&gt;.write(true)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;12. Why are &lt;code&gt;Path&lt;/code&gt; and &lt;code&gt;PathBuf&lt;/code&gt; built on &lt;code&gt;OsStr&lt;/code&gt; / &lt;code&gt;OsString&lt;/code&gt; instead of &lt;code&gt;str&lt;/code&gt; / &lt;code&gt;String&lt;/code&gt;?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;[ ] Because OS file paths are not guaranteed to be valid UTF-8&lt;/li&gt;
&lt;li&gt;[ ] Because &lt;code&gt;String&lt;/code&gt; cannot contain slash characters in Rust&lt;/li&gt;
&lt;li&gt;[ ] Because &lt;code&gt;OsStr&lt;/code&gt; always fits in 8 bytes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;13. How do &lt;code&gt;File::open(path)&lt;/code&gt; and &lt;code&gt;File::create(path)&lt;/code&gt; differ from &lt;code&gt;File::options()&lt;/code&gt;?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;[ ] &lt;code&gt;File::open&lt;/code&gt; and &lt;code&gt;File::create&lt;/code&gt; use fixed flag presets, whereas &lt;code&gt;File::options()&lt;/code&gt; lets you combine flags freely&lt;/li&gt;
&lt;li&gt;[ ] &lt;code&gt;File::open&lt;/code&gt; only works on Linux while &lt;code&gt;File::options()&lt;/code&gt; only works on Windows&lt;/li&gt;
&lt;li&gt;[ ] &lt;code&gt;File::options()&lt;/code&gt; does not return &lt;code&gt;std::io::Result&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;14. What does each configuration method on &lt;code&gt;File::options()&lt;/code&gt; (such as &lt;code&gt;.read(true)&lt;/code&gt; or &lt;code&gt;.create(true)&lt;/code&gt;) return before &lt;code&gt;.open()&lt;/code&gt; is called?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;[ ] A ready-to-use &lt;code&gt;File&lt;/code&gt; handle&lt;/li&gt;
&lt;li&gt;[ ] &lt;code&gt;&amp;amp;mut Self&lt;/code&gt; so you can chain more options&lt;/li&gt;
&lt;li&gt;[ ] A boolean &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;15. What is a file descriptor (&lt;code&gt;fd&lt;/code&gt;) on Unix/POSIX systems?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;[ ] An integer index into the process's open-resource table used by the OS to identify an open file, socket, or pipe&lt;/li&gt;
&lt;li&gt;[ ] A filename together with its extension&lt;/li&gt;
&lt;li&gt;[ ] A hidden metadata file stored next to the binary&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;16. How does &lt;code&gt;std::fs::File&lt;/code&gt; prevent file descriptor leaks in Rust?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;[ ] By periodically restarting the process&lt;/li&gt;
&lt;li&gt;[ ] Through RAII and &lt;code&gt;Drop&lt;/code&gt;: when &lt;code&gt;File&lt;/code&gt; leaves scope, the OS descriptor is closed automatically&lt;/li&gt;
&lt;li&gt;[ ] By capping every file at 1 KB&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;17. Which three standard file descriptors are open by default when a process starts?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;[ ] &lt;code&gt;0&lt;/code&gt; for &lt;code&gt;stdin&lt;/code&gt;, &lt;code&gt;1&lt;/code&gt; for &lt;code&gt;stdout&lt;/code&gt;, &lt;code&gt;2&lt;/code&gt; for &lt;code&gt;stderr&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;[ ] &lt;code&gt;1&lt;/code&gt; for &lt;code&gt;config&lt;/code&gt;, &lt;code&gt;2&lt;/code&gt; for &lt;code&gt;temp&lt;/code&gt;, &lt;code&gt;3&lt;/code&gt; for &lt;code&gt;log&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;[ ] None; a process must open every stream manually&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://eagle2000.space/en/rust/rust-files-tempfile-and-docs/" rel="noopener noreferrer"&gt;eagle2000.space&lt;/a&gt; with runnable Rust Playground slides and an interactive quiz.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>rust</category>
      <category>dev</category>
      <category>files</category>
      <category>builderpattern</category>
    </item>
  </channel>
</rss>
