<!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="API documentation for the Rust `path` mod in crate `std`."> <meta name="keywords" content="rust, rustlang, rust-lang, path"> <title>std::path - Rust</title> <link rel="stylesheet" type="text/css" href="../../normalize.css"> <link rel="stylesheet" type="text/css" href="../../rustdoc.css"> <link rel="stylesheet" type="text/css" href="../../main.css"> <link rel="shortcut icon" href="https://doc.rust-lang.org/favicon.ico"> </head> <body class="rustdoc mod"> <!--[if lte IE 8]> <div class="warning"> This old browser is unsupported and will most likely display funky things. </div> <![endif]--> <nav class="sidebar"> <a href='../../std/index.html'><img src='https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png' alt='logo' width='100'></a> <p class='location'>Module path</p><div class="block items"><ul><li><a href="#structs">Structs</a></li><li><a href="#enums">Enums</a></li><li><a href="#constants">Constants</a></li><li><a href="#functions">Functions</a></li></ul></div><p class='location'><a href='../index.html'>std</a></p><script>window.sidebarCurrent = {name: 'path', ty: 'mod', relpath: '../'};</script><script defer src="../sidebar-items.js"></script> </nav> <nav class="sub"> <form class="search-form js-only"> <div class="search-container"> <input class="search-input" name="search" autocomplete="off" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"> </div> </form> </nav> <section id='main' class="content"> <h1 class='fqn'><span class='in-band'>Module <a href='../index.html'>std</a>::<wbr><a class="mod" href=''>path</a></span><span class='out-of-band'><span class='since' title='Stable since Rust version 1.0.0'>1.0.0</span><span id='render-detail'> <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs"> [<span class='inner'>−</span>] </a> </span><a class='srclink' href='../../src/std/path.rs.html#11-3921' title='goto source code'>[src]</a></span></h1> <div class='docblock'><p>Cross-platform path manipulation.</p> <p>This module provides two types, <a href="../../std/path/struct.PathBuf.html"><code>PathBuf</code></a> and <a href="../../std/path/struct.Path.html"><code>Path</code></a> (akin to <a href="../../std/string/struct.String.html"><code>String</code></a> and <a href="../../std/primitive.str.html"><code>str</code></a>), for working with paths abstractly. These types are thin wrappers around <a href="../../std/ffi/struct.OsString.html"><code>OsString</code></a> and <a href="../../std/ffi/struct.OsStr.html"><code>OsStr</code></a> respectively, meaning that they work directly on strings according to the local platform's path syntax.</p> <p>Paths can be parsed into <a href="../../std/path/enum.Component.html"><code>Component</code></a>s by iterating over the structure returned by the <a href="../../std/path/struct.Path.html#method.components"><code>components</code></a> method on <a href="../../std/path/struct.Path.html"><code>Path</code></a>. <a href="../../std/path/enum.Component.html"><code>Component</code></a>s roughly correspond to the substrings between path separators (<code>/</code> or <code>\</code>). You can reconstruct an equivalent path from components with the <a href="../../std/path/struct.PathBuf.html#method.push"><code>push</code></a> method on <a href="../../std/path/struct.PathBuf.html"><code>PathBuf</code></a>; note that the paths may differ syntactically by the normalization described in the documentation for the <a href="../../std/path/struct.Path.html#method.components"><code>components</code></a> method.</p> <h2 id='simple-usage' class='section-header'><a href='#simple-usage'>Simple usage</a></h2> <p>Path manipulation includes both parsing components from slices and building new owned paths.</p> <p>To parse a path, you can create a <a href="../../std/path/struct.Path.html"><code>Path</code></a> slice from a <a href="../../std/primitive.str.html"><code>str</code></a> slice and start asking questions:</p> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">path</span>::<span class="ident">Path</span>; <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">ffi</span>::<span class="ident">OsStr</span>; <span class="kw">let</span> <span class="ident">path</span> <span class="op">=</span> <span class="ident">Path</span>::<span class="ident">new</span>(<span class="string">"/tmp/foo/bar.txt"</span>); <span class="kw">let</span> <span class="ident">parent</span> <span class="op">=</span> <span class="ident">path</span>.<span class="ident">parent</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">parent</span>, <span class="prelude-val">Some</span>(<span class="ident">Path</span>::<span class="ident">new</span>(<span class="string">"/tmp/foo"</span>))); <span class="kw">let</span> <span class="ident">file_stem</span> <span class="op">=</span> <span class="ident">path</span>.<span class="ident">file_stem</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">file_stem</span>, <span class="prelude-val">Some</span>(<span class="ident">OsStr</span>::<span class="ident">new</span>(<span class="string">"bar"</span>))); <span class="kw">let</span> <span class="ident">extension</span> <span class="op">=</span> <span class="ident">path</span>.<span class="ident">extension</span>(); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">extension</span>, <span class="prelude-val">Some</span>(<span class="ident">OsStr</span>::<span class="ident">new</span>(<span class="string">"txt"</span>)));<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=fn%20main()%20%7B%0Ause%20std%3A%3Apath%3A%3APath%3B%0Ause%20std%3A%3Affi%3A%3AOsStr%3B%0A%0Alet%20path%20%3D%20Path%3A%3Anew(%22%2Ftmp%2Ffoo%2Fbar.txt%22)%3B%0A%0Alet%20parent%20%3D%20path.parent()%3B%0Aassert_eq!(parent%2C%20Some(Path%3A%3Anew(%22%2Ftmp%2Ffoo%22)))%3B%0A%0Alet%20file_stem%20%3D%20path.file_stem()%3B%0Aassert_eq!(file_stem%2C%20Some(OsStr%3A%3Anew(%22bar%22)))%3B%0A%0Alet%20extension%20%3D%20path.extension()%3B%0Aassert_eq!(extension%2C%20Some(OsStr%3A%3Anew(%22txt%22)))%3B%0A%7D">Run</a></pre> <p>To build or modify paths, use <a href="../../std/path/struct.PathBuf.html"><code>PathBuf</code></a>:</p> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">path</span>::<span class="ident">PathBuf</span>; <span class="comment">// This way works...</span> <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">path</span> <span class="op">=</span> <span class="ident">PathBuf</span>::<span class="ident">from</span>(<span class="string">"c:\\"</span>); <span class="ident">path</span>.<span class="ident">push</span>(<span class="string">"windows"</span>); <span class="ident">path</span>.<span class="ident">push</span>(<span class="string">"system32"</span>); <span class="ident">path</span>.<span class="ident">set_extension</span>(<span class="string">"dll"</span>); <span class="comment">// ... but push is best used if you don't know everything up</span> <span class="comment">// front. If you do, this way is better:</span> <span class="kw">let</span> <span class="ident">path</span>: <span class="ident">PathBuf</span> <span class="op">=</span> [<span class="string">"c:\\"</span>, <span class="string">"windows"</span>, <span class="string">"system32.dll"</span>].<span class="ident">iter</span>().<span class="ident">collect</span>();<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=fn%20main()%20%7B%0Ause%20std%3A%3Apath%3A%3APathBuf%3B%0A%0A%2F%2F%20This%20way%20works...%0Alet%20mut%20path%20%3D%20PathBuf%3A%3Afrom(%22c%3A%5C%5C%22)%3B%0A%0Apath.push(%22windows%22)%3B%0Apath.push(%22system32%22)%3B%0A%0Apath.set_extension(%22dll%22)%3B%0A%0A%2F%2F%20...%20but%20push%20is%20best%20used%20if%20you%20don't%20know%20everything%20up%0A%2F%2F%20front.%20If%20you%20do%2C%20this%20way%20is%20better%3A%0Alet%20path%3A%20PathBuf%20%3D%20%5B%22c%3A%5C%5C%22%2C%20%22windows%22%2C%20%22system32.dll%22%5D.iter().collect()%3B%0A%7D">Run</a></pre> </div><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2> <table> <tr class=' module-item'> <td><a class="struct" href="struct.Components.html" title='struct std::path::Components'>Components</a></td> <td class='docblock-short'> <p>An interator over the <a href="enum.Component.html"><code>Component</code></a>s of a <a href="struct.Path.html"><code>Path</code></a>.</p> </td> </tr> <tr class=' module-item'> <td><a class="struct" href="struct.Display.html" title='struct std::path::Display'>Display</a></td> <td class='docblock-short'> <p>Helper struct for safely printing paths with <a href="../../std/macro.format.html"><code>format!</code></a> and <code>{}</code>.</p> </td> </tr> <tr class=' module-item'> <td><a class="struct" href="struct.Iter.html" title='struct std::path::Iter'>Iter</a></td> <td class='docblock-short'> <p>An iterator over the <a href="enum.Component.html"><code>Component</code></a>s of a <a href="struct.Path.html"><code>Path</code></a>, as <a href="../../std/ffi/struct.OsStr.html"><code>OsStr</code></a> slices.</p> </td> </tr> <tr class=' module-item'> <td><a class="struct" href="struct.Path.html" title='struct std::path::Path'>Path</a></td> <td class='docblock-short'> <p>A slice of a path (akin to <a href="../primitive.str.html"><code>str</code></a>).</p> </td> </tr> <tr class=' module-item'> <td><a class="struct" href="struct.PathBuf.html" title='struct std::path::PathBuf'>PathBuf</a></td> <td class='docblock-short'> <p>An owned, mutable path (akin to <a href="../string/struct.String.html"><code>String</code></a>).</p> </td> </tr> <tr class=' module-item'> <td><a class="struct" href="struct.PrefixComponent.html" title='struct std::path::PrefixComponent'>PrefixComponent</a></td> <td class='docblock-short'> <p>A structure wrapping a Windows path prefix as well as its unparsed string representation.</p> </td> </tr> <tr class=' module-item'> <td><a class="struct" href="struct.StripPrefixError.html" title='struct std::path::StripPrefixError'>StripPrefixError</a></td> <td class='docblock-short'> <p>An error returned from <a href="struct.Path.html#method.strip_prefix"><code>Path::strip_prefix</code></a> if the prefix was not found.</p> </td> </tr></table><h2 id='enums' class='section-header'><a href="#enums">Enums</a></h2> <table> <tr class=' module-item'> <td><a class="enum" href="enum.Component.html" title='enum std::path::Component'>Component</a></td> <td class='docblock-short'> <p>A single component of a path.</p> </td> </tr> <tr class=' module-item'> <td><a class="enum" href="enum.Prefix.html" title='enum std::path::Prefix'>Prefix</a></td> <td class='docblock-short'> <p>Windows path prefixes, e.g. <code>C:</code> or <code>\\server\share</code>.</p> </td> </tr></table><h2 id='constants' class='section-header'><a href="#constants">Constants</a></h2> <table> <tr class=' module-item'> <td><a class="constant" href="constant.MAIN_SEPARATOR.html" title='constant std::path::MAIN_SEPARATOR'>MAIN_SEPARATOR</a></td> <td class='docblock-short'> <p>The primary separator of path components for the current platform.</p> </td> </tr></table><h2 id='functions' class='section-header'><a href="#functions">Functions</a></h2> <table> <tr class=' module-item'> <td><a class="fn" href="fn.is_separator.html" title='fn std::path::is_separator'>is_separator</a></td> <td class='docblock-short'> <p>Determines whether the character is one of the permitted path separators for the current platform.</p> </td> </tr></table></section> <section id='search' class="content hidden"></section> <section class="footer"></section> <aside id="help" class="hidden"> <div> <h1 class="hidden">Help</h1> <div class="shortcuts"> <h2>Keyboard Shortcuts</h2> <dl> <dt>?</dt> <dd>Show this help dialog</dd> <dt>S</dt> <dd>Focus the search field</dd> <dt>⇤</dt> <dd>Move up in search results</dd> <dt>⇥</dt> <dd>Move down in search results</dd> <dt>⏎</dt> <dd>Go to active search result</dd> <dt>+</dt> <dd>Collapse/expand all sections</dd> </dl> </div> <div class="infos"> <h2>Search Tricks</h2> <p> Prefix searches with a type followed by a colon (e.g. <code>fn:</code>) to restrict the search to a given type. </p> <p> Accepted types are: <code>fn</code>, <code>mod</code>, <code>struct</code>, <code>enum</code>, <code>trait</code>, <code>type</code>, <code>macro</code>, and <code>const</code>. </p> <p> Search functions by type signature (e.g. <code>vec -> usize</code> or <code>* -> vec</code>) </p> </div> </div> </aside> <script> window.rootPath = "../../"; window.currentCrate = "std"; </script> <script src="../../main.js"></script> <script defer src="../../search-index.js"></script> </body> </html>