1 //! Cross-platform path manipulation.
3 //! This module provides two types, [`PathBuf`] and [`Path`] (akin to [`String`]
4 //! and [`str`]), for working with paths abstractly. These types are thin wrappers
5 //! around [`OsString`] and [`OsStr`] respectively, meaning that they work directly
6 //! on strings according to the local platform's path syntax.
8 //! Paths can be parsed into [`Component`]s by iterating over the structure
9 //! returned by the [`components`] method on [`Path`]. [`Component`]s roughly
10 //! correspond to the substrings between path separators (`/` or `\`). You can
11 //! reconstruct an equivalent path from components with the [`push`] method on
12 //! [`PathBuf`]; note that the paths may differ syntactically by the
13 //! normalization described in the documentation for the [`components`] method.
15 //! ## Case sensitivity
17 //! Unless otherwise indicated path methods that do not access the filesystem,
18 //! such as [`Path::starts_with`] and [`Path::ends_with`], are case sensitive no
19 //! matter the platform or filesystem. An exception to this is made for Windows
24 //! Path manipulation includes both parsing components from slices and building
27 //! To parse a path, you can create a [`Path`] slice from a [`str`]
28 //! slice and start asking questions:
31 //! use std::path::Path;
32 //! use std::ffi::OsStr;
34 //! let path = Path::new("/tmp/foo/bar.txt");
36 //! let parent = path.parent();
37 //! assert_eq!(parent, Some(Path::new("/tmp/foo")));
39 //! let file_stem = path.file_stem();
40 //! assert_eq!(file_stem, Some(OsStr::new("bar")));
42 //! let extension = path.extension();
43 //! assert_eq!(extension, Some(OsStr::new("txt")));
46 //! To build or modify paths, use [`PathBuf`]:
49 //! use std::path::PathBuf;
51 //! // This way works...
52 //! let mut path = PathBuf::from("c:\\");
54 //! path.push("windows");
55 //! path.push("system32");
57 //! path.set_extension("dll");
59 //! // ... but push is best used if you don't know everything up
60 //! // front. If you do, this way is better:
61 //! let path: PathBuf = ["c:\\", "windows", "system32.dll"].iter().collect();
64 //! [`components`]: Path::components
65 //! [`push`]: PathBuf::push
67 #![stable(feature = "rust1", since = "1.0.0")]
68 #![deny(unsafe_op_in_unsafe_fn)]
73 use crate::borrow::{Borrow, Cow};
75 use crate::collections::TryReserveError;
76 use crate::error::Error;
79 use crate::hash::{Hash, Hasher};
81 use crate::iter::{self, FusedIterator};
82 use crate::ops::{self, Deref};
84 use crate::str::FromStr;
87 use crate::ffi::{OsStr, OsString};
89 use crate::sys::path::{is_sep_byte, is_verbatim_sep, parse_prefix, MAIN_SEP_STR};
91 ////////////////////////////////////////////////////////////////////////////////
93 ////////////////////////////////////////////////////////////////////////////////
95 // Parsing in this module is done by directly transmuting OsStr to [u8] slices,
96 // taking advantage of the fact that OsStr always encodes ASCII characters
97 // as-is. Eventually, this transmutation should be replaced by direct uses of
98 // OsStr APIs for parsing, but it will take a while for those to become
101 ////////////////////////////////////////////////////////////////////////////////
103 ////////////////////////////////////////////////////////////////////////////////
105 /// Windows path prefixes, e.g., `C:` or `\\server\share`.
107 /// Windows uses a variety of path prefix styles, including references to drive
108 /// volumes (like `C:`), network shared folders (like `\\server\share`), and
109 /// others. In addition, some path prefixes are "verbatim" (i.e., prefixed with
110 /// `\\?\`), in which case `/` is *not* treated as a separator and essentially
111 /// no normalization is performed.
116 /// use std::path::{Component, Path, Prefix};
117 /// use std::path::Prefix::*;
118 /// use std::ffi::OsStr;
120 /// fn get_path_prefix(s: &str) -> Prefix {
121 /// let path = Path::new(s);
122 /// match path.components().next().unwrap() {
123 /// Component::Prefix(prefix_component) => prefix_component.kind(),
128 /// # if cfg!(windows) {
129 /// assert_eq!(Verbatim(OsStr::new("pictures")),
130 /// get_path_prefix(r"\\?\pictures\kittens"));
131 /// assert_eq!(VerbatimUNC(OsStr::new("server"), OsStr::new("share")),
132 /// get_path_prefix(r"\\?\UNC\server\share"));
133 /// assert_eq!(VerbatimDisk(b'C'), get_path_prefix(r"\\?\c:\"));
134 /// assert_eq!(DeviceNS(OsStr::new("BrainInterface")),
135 /// get_path_prefix(r"\\.\BrainInterface"));
136 /// assert_eq!(UNC(OsStr::new("server"), OsStr::new("share")),
137 /// get_path_prefix(r"\\server\share"));
138 /// assert_eq!(Disk(b'C'), get_path_prefix(r"C:\Users\Rust\Pictures\Ferris"));
141 #[derive(Copy, Clone, Debug, Hash, PartialOrd, Ord, PartialEq, Eq)]
142 #[stable(feature = "rust1", since = "1.0.0")]
143 pub enum Prefix<'a> {
144 /// Verbatim prefix, e.g., `\\?\cat_pics`.
146 /// Verbatim prefixes consist of `\\?\` immediately followed by the given
148 #[stable(feature = "rust1", since = "1.0.0")]
149 Verbatim(#[stable(feature = "rust1", since = "1.0.0")] &'a OsStr),
151 /// Verbatim prefix using Windows' _**U**niform **N**aming **C**onvention_,
152 /// e.g., `\\?\UNC\server\share`.
154 /// Verbatim UNC prefixes consist of `\\?\UNC\` immediately followed by the
155 /// server's hostname and a share name.
156 #[stable(feature = "rust1", since = "1.0.0")]
158 #[stable(feature = "rust1", since = "1.0.0")] &'a OsStr,
159 #[stable(feature = "rust1", since = "1.0.0")] &'a OsStr,
162 /// Verbatim disk prefix, e.g., `\\?\C:`.
164 /// Verbatim disk prefixes consist of `\\?\` immediately followed by the
165 /// drive letter and `:`.
166 #[stable(feature = "rust1", since = "1.0.0")]
167 VerbatimDisk(#[stable(feature = "rust1", since = "1.0.0")] u8),
169 /// Device namespace prefix, e.g., `\\.\COM42`.
171 /// Device namespace prefixes consist of `\\.\` immediately followed by the
173 #[stable(feature = "rust1", since = "1.0.0")]
174 DeviceNS(#[stable(feature = "rust1", since = "1.0.0")] &'a OsStr),
176 /// Prefix using Windows' _**U**niform **N**aming **C**onvention_, e.g.
177 /// `\\server\share`.
179 /// UNC prefixes consist of the server's hostname and a share name.
180 #[stable(feature = "rust1", since = "1.0.0")]
182 #[stable(feature = "rust1", since = "1.0.0")] &'a OsStr,
183 #[stable(feature = "rust1", since = "1.0.0")] &'a OsStr,
186 /// Prefix `C:` for the given disk drive.
187 #[stable(feature = "rust1", since = "1.0.0")]
188 Disk(#[stable(feature = "rust1", since = "1.0.0")] u8),
191 impl<'a> Prefix<'a> {
193 fn len(&self) -> usize {
195 fn os_str_len(s: &OsStr) -> usize {
196 os_str_as_u8_slice(s).len()
199 Verbatim(x) => 4 + os_str_len(x),
200 VerbatimUNC(x, y) => {
201 8 + os_str_len(x) + if os_str_len(y) > 0 { 1 + os_str_len(y) } else { 0 }
203 VerbatimDisk(_) => 6,
204 UNC(x, y) => 2 + os_str_len(x) + if os_str_len(y) > 0 { 1 + os_str_len(y) } else { 0 },
205 DeviceNS(x) => 4 + os_str_len(x),
210 /// Determines if the prefix is verbatim, i.e., begins with `\\?\`.
215 /// use std::path::Prefix::*;
216 /// use std::ffi::OsStr;
218 /// assert!(Verbatim(OsStr::new("pictures")).is_verbatim());
219 /// assert!(VerbatimUNC(OsStr::new("server"), OsStr::new("share")).is_verbatim());
220 /// assert!(VerbatimDisk(b'C').is_verbatim());
221 /// assert!(!DeviceNS(OsStr::new("BrainInterface")).is_verbatim());
222 /// assert!(!UNC(OsStr::new("server"), OsStr::new("share")).is_verbatim());
223 /// assert!(!Disk(b'C').is_verbatim());
227 #[stable(feature = "rust1", since = "1.0.0")]
228 pub fn is_verbatim(&self) -> bool {
230 matches!(*self, Verbatim(_) | VerbatimDisk(_) | VerbatimUNC(..))
234 fn is_drive(&self) -> bool {
235 matches!(*self, Prefix::Disk(_))
239 fn has_implicit_root(&self) -> bool {
244 ////////////////////////////////////////////////////////////////////////////////
245 // Exposed parsing helpers
246 ////////////////////////////////////////////////////////////////////////////////
248 /// Determines whether the character is one of the permitted path
249 /// separators for the current platform.
256 /// assert!(path::is_separator('/')); // '/' works for both Unix and Windows
257 /// assert!(!path::is_separator('❤'));
260 #[stable(feature = "rust1", since = "1.0.0")]
261 pub fn is_separator(c: char) -> bool {
262 c.is_ascii() && is_sep_byte(c as u8)
265 /// The primary separator of path components for the current platform.
267 /// For example, `/` on Unix and `\` on Windows.
268 #[stable(feature = "rust1", since = "1.0.0")]
269 pub const MAIN_SEPARATOR: char = crate::sys::path::MAIN_SEP;
271 /// The primary separator of path components for the current platform.
273 /// For example, `/` on Unix and `\` on Windows.
274 #[unstable(feature = "main_separator_str", issue = "94071")]
275 pub const MAIN_SEPARATOR_STR: &str = crate::sys::path::MAIN_SEP_STR;
277 ////////////////////////////////////////////////////////////////////////////////
279 ////////////////////////////////////////////////////////////////////////////////
281 // Iterate through `iter` while it matches `prefix`; return `None` if `prefix`
282 // is not a prefix of `iter`, otherwise return `Some(iter_after_prefix)` giving
283 // `iter` after having exhausted `prefix`.
284 fn iter_after<'a, 'b, I, J>(mut iter: I, mut prefix: J) -> Option<I>
286 I: Iterator<Item = Component<'a>> + Clone,
287 J: Iterator<Item = Component<'b>>,
290 let mut iter_next = iter.clone();
291 match (iter_next.next(), prefix.next()) {
292 (Some(ref x), Some(ref y)) if x == y => (),
293 (Some(_), Some(_)) => return None,
294 (Some(_), None) => return Some(iter),
295 (None, None) => return Some(iter),
296 (None, Some(_)) => return None,
302 // See note at the top of this module to understand why these are used:
304 // These casts are safe as OsStr is internally a wrapper around [u8] on all
307 // Note that currently this relies on the special knowledge that libstd has;
308 // these types are single-element structs but are not marked repr(transparent)
309 // or repr(C) which would make these casts allowable outside std.
310 fn os_str_as_u8_slice(s: &OsStr) -> &[u8] {
311 unsafe { &*(s as *const OsStr as *const [u8]) }
313 unsafe fn u8_slice_as_os_str(s: &[u8]) -> &OsStr {
314 // SAFETY: see the comment of `os_str_as_u8_slice`
315 unsafe { &*(s as *const [u8] as *const OsStr) }
318 // Detect scheme on Redox
319 fn has_redox_scheme(s: &[u8]) -> bool {
320 cfg!(target_os = "redox") && s.contains(&b':')
323 ////////////////////////////////////////////////////////////////////////////////
324 // Cross-platform, iterator-independent parsing
325 ////////////////////////////////////////////////////////////////////////////////
327 /// Says whether the first byte after the prefix is a separator.
328 fn has_physical_root(s: &[u8], prefix: Option<Prefix<'_>>) -> bool {
329 let path = if let Some(p) = prefix { &s[p.len()..] } else { s };
330 !path.is_empty() && is_sep_byte(path[0])
333 // basic workhorse for splitting stem and extension
334 fn rsplit_file_at_dot(file: &OsStr) -> (Option<&OsStr>, Option<&OsStr>) {
335 if os_str_as_u8_slice(file) == b".." {
336 return (Some(file), None);
339 // The unsafety here stems from converting between &OsStr and &[u8]
340 // and back. This is safe to do because (1) we only look at ASCII
341 // contents of the encoding and (2) new &OsStr values are produced
342 // only from ASCII-bounded slices of existing &OsStr values.
343 let mut iter = os_str_as_u8_slice(file).rsplitn(2, |b| *b == b'.');
344 let after = iter.next();
345 let before = iter.next();
346 if before == Some(b"") {
349 unsafe { (before.map(|s| u8_slice_as_os_str(s)), after.map(|s| u8_slice_as_os_str(s))) }
353 fn split_file_at_dot(file: &OsStr) -> (&OsStr, Option<&OsStr>) {
354 let slice = os_str_as_u8_slice(file);
359 // The unsafety here stems from converting between &OsStr and &[u8]
360 // and back. This is safe to do because (1) we only look at ASCII
361 // contents of the encoding and (2) new &OsStr values are produced
362 // only from ASCII-bounded slices of existing &OsStr values.
363 let i = match slice[1..].iter().position(|b| *b == b'.') {
365 None => return (file, None),
367 let before = &slice[..i];
368 let after = &slice[i + 1..];
369 unsafe { (u8_slice_as_os_str(before), Some(u8_slice_as_os_str(after))) }
372 ////////////////////////////////////////////////////////////////////////////////
373 // The core iterators
374 ////////////////////////////////////////////////////////////////////////////////
376 /// Component parsing works by a double-ended state machine; the cursors at the
377 /// front and back of the path each keep track of what parts of the path have
378 /// been consumed so far.
380 /// Going front to back, a path is made up of a prefix, a starting
381 /// directory component, and a body (of normal components)
382 #[derive(Copy, Clone, PartialEq, PartialOrd, Debug)]
385 StartDir = 1, // / or . or nothing
386 Body = 2, // foo/bar/baz
390 /// A structure wrapping a Windows path prefix as well as its unparsed string
393 /// In addition to the parsed [`Prefix`] information returned by [`kind`],
394 /// `PrefixComponent` also holds the raw and unparsed [`OsStr`] slice,
395 /// returned by [`as_os_str`].
397 /// Instances of this `struct` can be obtained by matching against the
398 /// [`Prefix` variant] on [`Component`].
400 /// Does not occur on Unix.
405 /// # if cfg!(windows) {
406 /// use std::path::{Component, Path, Prefix};
407 /// use std::ffi::OsStr;
409 /// let path = Path::new(r"c:\you\later\");
410 /// match path.components().next().unwrap() {
411 /// Component::Prefix(prefix_component) => {
412 /// assert_eq!(Prefix::Disk(b'C'), prefix_component.kind());
413 /// assert_eq!(OsStr::new("c:"), prefix_component.as_os_str());
415 /// _ => unreachable!(),
420 /// [`as_os_str`]: PrefixComponent::as_os_str
421 /// [`kind`]: PrefixComponent::kind
422 /// [`Prefix` variant]: Component::Prefix
423 #[stable(feature = "rust1", since = "1.0.0")]
424 #[derive(Copy, Clone, Eq, Debug)]
425 pub struct PrefixComponent<'a> {
426 /// The prefix as an unparsed `OsStr` slice.
429 /// The parsed prefix data.
433 impl<'a> PrefixComponent<'a> {
434 /// Returns the parsed prefix data.
436 /// See [`Prefix`]'s documentation for more information on the different
437 /// kinds of prefixes.
438 #[stable(feature = "rust1", since = "1.0.0")]
441 pub fn kind(&self) -> Prefix<'a> {
445 /// Returns the raw [`OsStr`] slice for this prefix.
446 #[stable(feature = "rust1", since = "1.0.0")]
449 pub fn as_os_str(&self) -> &'a OsStr {
454 #[stable(feature = "rust1", since = "1.0.0")]
455 impl<'a> cmp::PartialEq for PrefixComponent<'a> {
457 fn eq(&self, other: &PrefixComponent<'a>) -> bool {
458 cmp::PartialEq::eq(&self.parsed, &other.parsed)
462 #[stable(feature = "rust1", since = "1.0.0")]
463 impl<'a> cmp::PartialOrd for PrefixComponent<'a> {
465 fn partial_cmp(&self, other: &PrefixComponent<'a>) -> Option<cmp::Ordering> {
466 cmp::PartialOrd::partial_cmp(&self.parsed, &other.parsed)
470 #[stable(feature = "rust1", since = "1.0.0")]
471 impl cmp::Ord for PrefixComponent<'_> {
473 fn cmp(&self, other: &Self) -> cmp::Ordering {
474 cmp::Ord::cmp(&self.parsed, &other.parsed)
478 #[stable(feature = "rust1", since = "1.0.0")]
479 impl Hash for PrefixComponent<'_> {
480 fn hash<H: Hasher>(&self, h: &mut H) {
485 /// A single component of a path.
487 /// A `Component` roughly corresponds to a substring between path separators
490 /// This `enum` is created by iterating over [`Components`], which in turn is
491 /// created by the [`components`](Path::components) method on [`Path`].
496 /// use std::path::{Component, Path};
498 /// let path = Path::new("/tmp/foo/bar.txt");
499 /// let components = path.components().collect::<Vec<_>>();
500 /// assert_eq!(&components, &[
501 /// Component::RootDir,
502 /// Component::Normal("tmp".as_ref()),
503 /// Component::Normal("foo".as_ref()),
504 /// Component::Normal("bar.txt".as_ref()),
507 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
508 #[stable(feature = "rust1", since = "1.0.0")]
509 pub enum Component<'a> {
510 /// A Windows path prefix, e.g., `C:` or `\\server\share`.
512 /// There is a large variety of prefix types, see [`Prefix`]'s documentation
515 /// Does not occur on Unix.
516 #[stable(feature = "rust1", since = "1.0.0")]
517 Prefix(#[stable(feature = "rust1", since = "1.0.0")] PrefixComponent<'a>),
519 /// The root directory component, appears after any prefix and before anything else.
521 /// It represents a separator that designates that a path starts from root.
522 #[stable(feature = "rust1", since = "1.0.0")]
525 /// A reference to the current directory, i.e., `.`.
526 #[stable(feature = "rust1", since = "1.0.0")]
529 /// A reference to the parent directory, i.e., `..`.
530 #[stable(feature = "rust1", since = "1.0.0")]
533 /// A normal component, e.g., `a` and `b` in `a/b`.
535 /// This variant is the most common one, it represents references to files
537 #[stable(feature = "rust1", since = "1.0.0")]
538 Normal(#[stable(feature = "rust1", since = "1.0.0")] &'a OsStr),
541 impl<'a> Component<'a> {
542 /// Extracts the underlying [`OsStr`] slice.
547 /// use std::path::Path;
549 /// let path = Path::new("./tmp/foo/bar.txt");
550 /// let components: Vec<_> = path.components().map(|comp| comp.as_os_str()).collect();
551 /// assert_eq!(&components, &[".", "tmp", "foo", "bar.txt"]);
553 #[must_use = "`self` will be dropped if the result is not used"]
554 #[stable(feature = "rust1", since = "1.0.0")]
555 pub fn as_os_str(self) -> &'a OsStr {
557 Component::Prefix(p) => p.as_os_str(),
558 Component::RootDir => OsStr::new(MAIN_SEP_STR),
559 Component::CurDir => OsStr::new("."),
560 Component::ParentDir => OsStr::new(".."),
561 Component::Normal(path) => path,
566 #[stable(feature = "rust1", since = "1.0.0")]
567 impl AsRef<OsStr> for Component<'_> {
569 fn as_ref(&self) -> &OsStr {
574 #[stable(feature = "path_component_asref", since = "1.25.0")]
575 impl AsRef<Path> for Component<'_> {
577 fn as_ref(&self) -> &Path {
578 self.as_os_str().as_ref()
582 /// An iterator over the [`Component`]s of a [`Path`].
584 /// This `struct` is created by the [`components`] method on [`Path`].
585 /// See its documentation for more.
590 /// use std::path::Path;
592 /// let path = Path::new("/tmp/foo/bar.txt");
594 /// for component in path.components() {
595 /// println!("{component:?}");
599 /// [`components`]: Path::components
601 #[must_use = "iterators are lazy and do nothing unless consumed"]
602 #[stable(feature = "rust1", since = "1.0.0")]
603 pub struct Components<'a> {
604 // The path left to parse components from
607 // The prefix as it was originally parsed, if any
608 prefix: Option<Prefix<'a>>,
610 // true if path *physically* has a root separator; for most Windows
611 // prefixes, it may have a "logical" root separator for the purposes of
612 // normalization, e.g., \\server\share == \\server\share\.
613 has_physical_root: bool,
615 // The iterator is double-ended, and these two states keep track of what has
616 // been produced from either end
621 /// An iterator over the [`Component`]s of a [`Path`], as [`OsStr`] slices.
623 /// This `struct` is created by the [`iter`] method on [`Path`].
624 /// See its documentation for more.
626 /// [`iter`]: Path::iter
628 #[must_use = "iterators are lazy and do nothing unless consumed"]
629 #[stable(feature = "rust1", since = "1.0.0")]
630 pub struct Iter<'a> {
631 inner: Components<'a>,
634 #[stable(feature = "path_components_debug", since = "1.13.0")]
635 impl fmt::Debug for Components<'_> {
636 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
637 struct DebugHelper<'a>(&'a Path);
639 impl fmt::Debug for DebugHelper<'_> {
640 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
641 f.debug_list().entries(self.0.components()).finish()
645 f.debug_tuple("Components").field(&DebugHelper(self.as_path())).finish()
649 impl<'a> Components<'a> {
650 // how long is the prefix, if any?
652 fn prefix_len(&self) -> usize {
653 self.prefix.as_ref().map(Prefix::len).unwrap_or(0)
657 fn prefix_verbatim(&self) -> bool {
658 self.prefix.as_ref().map(Prefix::is_verbatim).unwrap_or(false)
661 /// how much of the prefix is left from the point of view of iteration?
663 fn prefix_remaining(&self) -> usize {
664 if self.front == State::Prefix { self.prefix_len() } else { 0 }
667 // Given the iteration so far, how much of the pre-State::Body path is left?
669 fn len_before_body(&self) -> usize {
670 let root = if self.front <= State::StartDir && self.has_physical_root { 1 } else { 0 };
671 let cur_dir = if self.front <= State::StartDir && self.include_cur_dir() { 1 } else { 0 };
672 self.prefix_remaining() + root + cur_dir
675 // is the iteration complete?
677 fn finished(&self) -> bool {
678 self.front == State::Done || self.back == State::Done || self.front > self.back
682 fn is_sep_byte(&self, b: u8) -> bool {
683 if self.prefix_verbatim() { is_verbatim_sep(b) } else { is_sep_byte(b) }
686 /// Extracts a slice corresponding to the portion of the path remaining for iteration.
691 /// use std::path::Path;
693 /// let mut components = Path::new("/tmp/foo/bar.txt").components();
694 /// components.next();
695 /// components.next();
697 /// assert_eq!(Path::new("foo/bar.txt"), components.as_path());
700 #[stable(feature = "rust1", since = "1.0.0")]
701 pub fn as_path(&self) -> &'a Path {
702 let mut comps = self.clone();
703 if comps.front == State::Body {
706 if comps.back == State::Body {
709 unsafe { Path::from_u8_slice(comps.path) }
712 /// Is the *original* path rooted?
713 fn has_root(&self) -> bool {
714 if self.has_physical_root {
717 if let Some(p) = self.prefix {
718 if p.has_implicit_root() {
725 /// Should the normalized path include a leading . ?
726 fn include_cur_dir(&self) -> bool {
730 let mut iter = self.path[self.prefix_len()..].iter();
731 match (iter.next(), iter.next()) {
732 (Some(&b'.'), None) => true,
733 (Some(&b'.'), Some(&b)) => self.is_sep_byte(b),
738 // parse a given byte sequence into the corresponding path component
739 fn parse_single_component<'b>(&self, comp: &'b [u8]) -> Option<Component<'b>> {
741 b"." if self.prefix_verbatim() => Some(Component::CurDir),
742 b"." => None, // . components are normalized away, except at
743 // the beginning of a path, which is treated
744 // separately via `include_cur_dir`
745 b".." => Some(Component::ParentDir),
747 _ => Some(Component::Normal(unsafe { u8_slice_as_os_str(comp) })),
751 // parse a component from the left, saying how many bytes to consume to
752 // remove the component
753 fn parse_next_component(&self) -> (usize, Option<Component<'a>>) {
754 debug_assert!(self.front == State::Body);
755 let (extra, comp) = match self.path.iter().position(|b| self.is_sep_byte(*b)) {
756 None => (0, self.path),
757 Some(i) => (1, &self.path[..i]),
759 (comp.len() + extra, self.parse_single_component(comp))
762 // parse a component from the right, saying how many bytes to consume to
763 // remove the component
764 fn parse_next_component_back(&self) -> (usize, Option<Component<'a>>) {
765 debug_assert!(self.back == State::Body);
766 let start = self.len_before_body();
767 let (extra, comp) = match self.path[start..].iter().rposition(|b| self.is_sep_byte(*b)) {
768 None => (0, &self.path[start..]),
769 Some(i) => (1, &self.path[start + i + 1..]),
771 (comp.len() + extra, self.parse_single_component(comp))
774 // trim away repeated separators (i.e., empty components) on the left
775 fn trim_left(&mut self) {
776 while !self.path.is_empty() {
777 let (size, comp) = self.parse_next_component();
781 self.path = &self.path[size..];
786 // trim away repeated separators (i.e., empty components) on the right
787 fn trim_right(&mut self) {
788 while self.path.len() > self.len_before_body() {
789 let (size, comp) = self.parse_next_component_back();
793 self.path = &self.path[..self.path.len() - size];
799 #[stable(feature = "rust1", since = "1.0.0")]
800 impl AsRef<Path> for Components<'_> {
802 fn as_ref(&self) -> &Path {
807 #[stable(feature = "rust1", since = "1.0.0")]
808 impl AsRef<OsStr> for Components<'_> {
810 fn as_ref(&self) -> &OsStr {
811 self.as_path().as_os_str()
815 #[stable(feature = "path_iter_debug", since = "1.13.0")]
816 impl fmt::Debug for Iter<'_> {
817 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
818 struct DebugHelper<'a>(&'a Path);
820 impl fmt::Debug for DebugHelper<'_> {
821 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
822 f.debug_list().entries(self.0.iter()).finish()
826 f.debug_tuple("Iter").field(&DebugHelper(self.as_path())).finish()
831 /// Extracts a slice corresponding to the portion of the path remaining for iteration.
836 /// use std::path::Path;
838 /// let mut iter = Path::new("/tmp/foo/bar.txt").iter();
842 /// assert_eq!(Path::new("foo/bar.txt"), iter.as_path());
844 #[stable(feature = "rust1", since = "1.0.0")]
847 pub fn as_path(&self) -> &'a Path {
852 #[stable(feature = "rust1", since = "1.0.0")]
853 impl AsRef<Path> for Iter<'_> {
855 fn as_ref(&self) -> &Path {
860 #[stable(feature = "rust1", since = "1.0.0")]
861 impl AsRef<OsStr> for Iter<'_> {
863 fn as_ref(&self) -> &OsStr {
864 self.as_path().as_os_str()
868 #[stable(feature = "rust1", since = "1.0.0")]
869 impl<'a> Iterator for Iter<'a> {
870 type Item = &'a OsStr;
873 fn next(&mut self) -> Option<&'a OsStr> {
874 self.inner.next().map(Component::as_os_str)
878 #[stable(feature = "rust1", since = "1.0.0")]
879 impl<'a> DoubleEndedIterator for Iter<'a> {
881 fn next_back(&mut self) -> Option<&'a OsStr> {
882 self.inner.next_back().map(Component::as_os_str)
886 #[stable(feature = "fused", since = "1.26.0")]
887 impl FusedIterator for Iter<'_> {}
889 #[stable(feature = "rust1", since = "1.0.0")]
890 impl<'a> Iterator for Components<'a> {
891 type Item = Component<'a>;
893 fn next(&mut self) -> Option<Component<'a>> {
894 while !self.finished() {
896 State::Prefix if self.prefix_len() > 0 => {
897 self.front = State::StartDir;
898 debug_assert!(self.prefix_len() <= self.path.len());
899 let raw = &self.path[..self.prefix_len()];
900 self.path = &self.path[self.prefix_len()..];
901 return Some(Component::Prefix(PrefixComponent {
902 raw: unsafe { u8_slice_as_os_str(raw) },
903 parsed: self.prefix.unwrap(),
907 self.front = State::StartDir;
910 self.front = State::Body;
911 if self.has_physical_root {
912 debug_assert!(!self.path.is_empty());
913 self.path = &self.path[1..];
914 return Some(Component::RootDir);
915 } else if let Some(p) = self.prefix {
916 if p.has_implicit_root() && !p.is_verbatim() {
917 return Some(Component::RootDir);
919 } else if self.include_cur_dir() {
920 debug_assert!(!self.path.is_empty());
921 self.path = &self.path[1..];
922 return Some(Component::CurDir);
925 State::Body if !self.path.is_empty() => {
926 let (size, comp) = self.parse_next_component();
927 self.path = &self.path[size..];
933 self.front = State::Done;
935 State::Done => unreachable!(),
942 #[stable(feature = "rust1", since = "1.0.0")]
943 impl<'a> DoubleEndedIterator for Components<'a> {
944 fn next_back(&mut self) -> Option<Component<'a>> {
945 while !self.finished() {
947 State::Body if self.path.len() > self.len_before_body() => {
948 let (size, comp) = self.parse_next_component_back();
949 self.path = &self.path[..self.path.len() - size];
955 self.back = State::StartDir;
958 self.back = State::Prefix;
959 if self.has_physical_root {
960 self.path = &self.path[..self.path.len() - 1];
961 return Some(Component::RootDir);
962 } else if let Some(p) = self.prefix {
963 if p.has_implicit_root() && !p.is_verbatim() {
964 return Some(Component::RootDir);
966 } else if self.include_cur_dir() {
967 self.path = &self.path[..self.path.len() - 1];
968 return Some(Component::CurDir);
971 State::Prefix if self.prefix_len() > 0 => {
972 self.back = State::Done;
973 return Some(Component::Prefix(PrefixComponent {
974 raw: unsafe { u8_slice_as_os_str(self.path) },
975 parsed: self.prefix.unwrap(),
979 self.back = State::Done;
982 State::Done => unreachable!(),
989 #[stable(feature = "fused", since = "1.26.0")]
990 impl FusedIterator for Components<'_> {}
992 #[stable(feature = "rust1", since = "1.0.0")]
993 impl<'a> cmp::PartialEq for Components<'a> {
995 fn eq(&self, other: &Components<'a>) -> bool {
996 let Components { path: _, front: _, back: _, has_physical_root: _, prefix: _ } = self;
998 // Fast path for exact matches, e.g. for hashmap lookups.
999 // Don't explicitly compare the prefix or has_physical_root fields since they'll
1000 // either be covered by the `path` buffer or are only relevant for `prefix_verbatim()`.
1001 if self.path.len() == other.path.len()
1002 && self.front == other.front
1003 && self.back == State::Body
1004 && other.back == State::Body
1005 && self.prefix_verbatim() == other.prefix_verbatim()
1007 // possible future improvement: this could bail out earlier if there were a
1008 // reverse memcmp/bcmp comparing back to front
1009 if self.path == other.path {
1014 // compare back to front since absolute paths often share long prefixes
1015 Iterator::eq(self.clone().rev(), other.clone().rev())
1019 #[stable(feature = "rust1", since = "1.0.0")]
1020 impl cmp::Eq for Components<'_> {}
1022 #[stable(feature = "rust1", since = "1.0.0")]
1023 impl<'a> cmp::PartialOrd for Components<'a> {
1025 fn partial_cmp(&self, other: &Components<'a>) -> Option<cmp::Ordering> {
1026 Some(compare_components(self.clone(), other.clone()))
1030 #[stable(feature = "rust1", since = "1.0.0")]
1031 impl cmp::Ord for Components<'_> {
1033 fn cmp(&self, other: &Self) -> cmp::Ordering {
1034 compare_components(self.clone(), other.clone())
1038 fn compare_components(mut left: Components<'_>, mut right: Components<'_>) -> cmp::Ordering {
1039 // Fast path for long shared prefixes
1041 // - compare raw bytes to find first mismatch
1042 // - backtrack to find separator before mismatch to avoid ambiguous parsings of '.' or '..' characters
1043 // - if found update state to only do a component-wise comparison on the remainder,
1044 // otherwise do it on the full path
1046 // The fast path isn't taken for paths with a PrefixComponent to avoid backtracking into
1047 // the middle of one
1048 if left.prefix.is_none() && right.prefix.is_none() && left.front == right.front {
1049 // possible future improvement: a [u8]::first_mismatch simd implementation
1050 let first_difference = match left.path.iter().zip(right.path).position(|(&a, &b)| a != b) {
1051 None if left.path.len() == right.path.len() => return cmp::Ordering::Equal,
1052 None => left.path.len().min(right.path.len()),
1056 if let Some(previous_sep) =
1057 left.path[..first_difference].iter().rposition(|&b| left.is_sep_byte(b))
1059 let mismatched_component_start = previous_sep + 1;
1060 left.path = &left.path[mismatched_component_start..];
1061 left.front = State::Body;
1062 right.path = &right.path[mismatched_component_start..];
1063 right.front = State::Body;
1067 Iterator::cmp(left, right)
1070 /// An iterator over [`Path`] and its ancestors.
1072 /// This `struct` is created by the [`ancestors`] method on [`Path`].
1073 /// See its documentation for more.
1078 /// use std::path::Path;
1080 /// let path = Path::new("/foo/bar");
1082 /// for ancestor in path.ancestors() {
1083 /// println!("{}", ancestor.display());
1087 /// [`ancestors`]: Path::ancestors
1088 #[derive(Copy, Clone, Debug)]
1089 #[must_use = "iterators are lazy and do nothing unless consumed"]
1090 #[stable(feature = "path_ancestors", since = "1.28.0")]
1091 pub struct Ancestors<'a> {
1092 next: Option<&'a Path>,
1095 #[stable(feature = "path_ancestors", since = "1.28.0")]
1096 impl<'a> Iterator for Ancestors<'a> {
1097 type Item = &'a Path;
1100 fn next(&mut self) -> Option<Self::Item> {
1101 let next = self.next;
1102 self.next = next.and_then(Path::parent);
1107 #[stable(feature = "path_ancestors", since = "1.28.0")]
1108 impl FusedIterator for Ancestors<'_> {}
1110 ////////////////////////////////////////////////////////////////////////////////
1111 // Basic types and traits
1112 ////////////////////////////////////////////////////////////////////////////////
1114 /// An owned, mutable path (akin to [`String`]).
1116 /// This type provides methods like [`push`] and [`set_extension`] that mutate
1117 /// the path in place. It also implements [`Deref`] to [`Path`], meaning that
1118 /// all methods on [`Path`] slices are available on `PathBuf` values as well.
1120 /// [`push`]: PathBuf::push
1121 /// [`set_extension`]: PathBuf::set_extension
1123 /// More details about the overall approach can be found in
1124 /// the [module documentation](self).
1128 /// You can use [`push`] to build up a `PathBuf` from
1132 /// use std::path::PathBuf;
1134 /// let mut path = PathBuf::new();
1136 /// path.push(r"C:\");
1137 /// path.push("windows");
1138 /// path.push("system32");
1140 /// path.set_extension("dll");
1143 /// However, [`push`] is best used for dynamic situations. This is a better way
1144 /// to do this when you know all of the components ahead of time:
1147 /// use std::path::PathBuf;
1149 /// let path: PathBuf = [r"C:\", "windows", "system32.dll"].iter().collect();
1152 /// We can still do better than this! Since these are all strings, we can use
1156 /// use std::path::PathBuf;
1158 /// let path = PathBuf::from(r"C:\windows\system32.dll");
1161 /// Which method works best depends on what kind of situation you're in.
1162 #[cfg_attr(not(test), rustc_diagnostic_item = "PathBuf")]
1163 #[stable(feature = "rust1", since = "1.0.0")]
1165 // `PathBuf::as_mut_vec` current implementation relies
1166 // on `PathBuf` being layout-compatible with `Vec<u8>`.
1167 // When attribute privacy is implemented, `PathBuf` should be annotated as `#[repr(transparent)]`.
1168 // Anyway, `PathBuf` representation and layout are considered implementation detail, are
1169 // not documented and must not be relied upon.
1170 pub struct PathBuf {
1176 fn as_mut_vec(&mut self) -> &mut Vec<u8> {
1177 unsafe { &mut *(self as *mut PathBuf as *mut Vec<u8>) }
1180 /// Allocates an empty `PathBuf`.
1185 /// use std::path::PathBuf;
1187 /// let path = PathBuf::new();
1189 #[stable(feature = "rust1", since = "1.0.0")]
1192 pub fn new() -> PathBuf {
1193 PathBuf { inner: OsString::new() }
1196 /// Creates a new `PathBuf` with a given capacity used to create the
1197 /// internal [`OsString`]. See [`with_capacity`] defined on [`OsString`].
1202 /// use std::path::PathBuf;
1204 /// let mut path = PathBuf::with_capacity(10);
1205 /// let capacity = path.capacity();
1207 /// // This push is done without reallocating
1208 /// path.push(r"C:\");
1210 /// assert_eq!(capacity, path.capacity());
1213 /// [`with_capacity`]: OsString::with_capacity
1214 #[stable(feature = "path_buf_capacity", since = "1.44.0")]
1217 pub fn with_capacity(capacity: usize) -> PathBuf {
1218 PathBuf { inner: OsString::with_capacity(capacity) }
1221 /// Coerces to a [`Path`] slice.
1226 /// use std::path::{Path, PathBuf};
1228 /// let p = PathBuf::from("/test");
1229 /// assert_eq!(Path::new("/test"), p.as_path());
1231 #[stable(feature = "rust1", since = "1.0.0")]
1234 pub fn as_path(&self) -> &Path {
1238 /// Extends `self` with `path`.
1240 /// If `path` is absolute, it replaces the current path.
1244 /// * if `path` has a root but no prefix (e.g., `\windows`), it
1245 /// replaces everything except for the prefix (if any) of `self`.
1246 /// * if `path` has a prefix but no root, it replaces `self`.
1247 /// * if `self` has a verbatim prefix (e.g. `\\?\C:\windows`)
1248 /// and `path` is not empty, the new path is normalized: all references
1249 /// to `.` and `..` are removed.
1253 /// Pushing a relative path extends the existing path:
1256 /// use std::path::PathBuf;
1258 /// let mut path = PathBuf::from("/tmp");
1259 /// path.push("file.bk");
1260 /// assert_eq!(path, PathBuf::from("/tmp/file.bk"));
1263 /// Pushing an absolute path replaces the existing path:
1266 /// use std::path::PathBuf;
1268 /// let mut path = PathBuf::from("/tmp");
1269 /// path.push("/etc");
1270 /// assert_eq!(path, PathBuf::from("/etc"));
1272 #[stable(feature = "rust1", since = "1.0.0")]
1273 pub fn push<P: AsRef<Path>>(&mut self, path: P) {
1274 self._push(path.as_ref())
1277 fn _push(&mut self, path: &Path) {
1278 // in general, a separator is needed if the rightmost byte is not a separator
1279 let mut need_sep = self.as_mut_vec().last().map(|c| !is_sep_byte(*c)).unwrap_or(false);
1281 // in the special case of `C:` on Windows, do *not* add a separator
1282 let comps = self.components();
1284 if comps.prefix_len() > 0
1285 && comps.prefix_len() == comps.path.len()
1286 && comps.prefix.unwrap().is_drive()
1291 // absolute `path` replaces `self`
1292 if path.is_absolute() || path.prefix().is_some() {
1293 self.as_mut_vec().truncate(0);
1295 // verbatim paths need . and .. removed
1296 } else if comps.prefix_verbatim() && !path.inner.is_empty() {
1297 let mut buf: Vec<_> = comps.collect();
1298 for c in path.components() {
1300 Component::RootDir => {
1304 Component::CurDir => (),
1305 Component::ParentDir => {
1306 if let Some(Component::Normal(_)) = buf.last() {
1314 let mut res = OsString::new();
1315 let mut need_sep = false;
1318 if need_sep && c != Component::RootDir {
1319 res.push(MAIN_SEP_STR);
1321 res.push(c.as_os_str());
1323 need_sep = match c {
1324 Component::RootDir => false,
1325 Component::Prefix(prefix) => {
1326 !prefix.parsed.is_drive() && prefix.parsed.len() > 0
1335 // `path` has a root but no prefix, e.g., `\windows` (Windows only)
1336 } else if path.has_root() {
1337 let prefix_len = self.components().prefix_remaining();
1338 self.as_mut_vec().truncate(prefix_len);
1340 // `path` is a pure relative path
1341 } else if need_sep {
1342 self.inner.push(MAIN_SEP_STR);
1345 self.inner.push(path);
1348 /// Truncates `self` to [`self.parent`].
1350 /// Returns `false` and does nothing if [`self.parent`] is [`None`].
1351 /// Otherwise, returns `true`.
1353 /// [`self.parent`]: Path::parent
1358 /// use std::path::{Path, PathBuf};
1360 /// let mut p = PathBuf::from("/spirited/away.rs");
1363 /// assert_eq!(Path::new("/spirited"), p);
1365 /// assert_eq!(Path::new("/"), p);
1367 #[stable(feature = "rust1", since = "1.0.0")]
1368 pub fn pop(&mut self) -> bool {
1369 match self.parent().map(|p| p.as_u8_slice().len()) {
1371 self.as_mut_vec().truncate(len);
1378 /// Updates [`self.file_name`] to `file_name`.
1380 /// If [`self.file_name`] was [`None`], this is equivalent to pushing
1383 /// Otherwise it is equivalent to calling [`pop`] and then pushing
1384 /// `file_name`. The new path will be a sibling of the original path.
1385 /// (That is, it will have the same parent.)
1387 /// [`self.file_name`]: Path::file_name
1388 /// [`pop`]: PathBuf::pop
1393 /// use std::path::PathBuf;
1395 /// let mut buf = PathBuf::from("/");
1396 /// assert!(buf.file_name() == None);
1397 /// buf.set_file_name("bar");
1398 /// assert!(buf == PathBuf::from("/bar"));
1399 /// assert!(buf.file_name().is_some());
1400 /// buf.set_file_name("baz.txt");
1401 /// assert!(buf == PathBuf::from("/baz.txt"));
1403 #[stable(feature = "rust1", since = "1.0.0")]
1404 pub fn set_file_name<S: AsRef<OsStr>>(&mut self, file_name: S) {
1405 self._set_file_name(file_name.as_ref())
1408 fn _set_file_name(&mut self, file_name: &OsStr) {
1409 if self.file_name().is_some() {
1410 let popped = self.pop();
1411 debug_assert!(popped);
1413 self.push(file_name);
1416 /// Updates [`self.extension`] to `extension`.
1418 /// Returns `false` and does nothing if [`self.file_name`] is [`None`],
1419 /// returns `true` and updates the extension otherwise.
1421 /// If [`self.extension`] is [`None`], the extension is added; otherwise
1424 /// [`self.file_name`]: Path::file_name
1425 /// [`self.extension`]: Path::extension
1430 /// use std::path::{Path, PathBuf};
1432 /// let mut p = PathBuf::from("/feel/the");
1434 /// p.set_extension("force");
1435 /// assert_eq!(Path::new("/feel/the.force"), p.as_path());
1437 /// p.set_extension("dark_side");
1438 /// assert_eq!(Path::new("/feel/the.dark_side"), p.as_path());
1440 #[stable(feature = "rust1", since = "1.0.0")]
1441 pub fn set_extension<S: AsRef<OsStr>>(&mut self, extension: S) -> bool {
1442 self._set_extension(extension.as_ref())
1445 fn _set_extension(&mut self, extension: &OsStr) -> bool {
1446 let file_stem = match self.file_stem() {
1447 None => return false,
1448 Some(f) => os_str_as_u8_slice(f),
1451 // truncate until right after the file stem
1452 let end_file_stem = file_stem[file_stem.len()..].as_ptr() as usize;
1453 let start = os_str_as_u8_slice(&self.inner).as_ptr() as usize;
1454 let v = self.as_mut_vec();
1455 v.truncate(end_file_stem.wrapping_sub(start));
1457 // add the new extension, if any
1458 let new = os_str_as_u8_slice(extension);
1459 if !new.is_empty() {
1460 v.reserve_exact(new.len() + 1);
1462 v.extend_from_slice(new);
1468 /// Consumes the `PathBuf`, yielding its internal [`OsString`] storage.
1473 /// use std::path::PathBuf;
1475 /// let p = PathBuf::from("/the/head");
1476 /// let os_str = p.into_os_string();
1478 #[stable(feature = "rust1", since = "1.0.0")]
1479 #[must_use = "`self` will be dropped if the result is not used"]
1481 pub fn into_os_string(self) -> OsString {
1485 /// Converts this `PathBuf` into a [boxed](Box) [`Path`].
1486 #[stable(feature = "into_boxed_path", since = "1.20.0")]
1487 #[must_use = "`self` will be dropped if the result is not used"]
1489 pub fn into_boxed_path(self) -> Box<Path> {
1490 let rw = Box::into_raw(self.inner.into_boxed_os_str()) as *mut Path;
1491 unsafe { Box::from_raw(rw) }
1494 /// Invokes [`capacity`] on the underlying instance of [`OsString`].
1496 /// [`capacity`]: OsString::capacity
1497 #[stable(feature = "path_buf_capacity", since = "1.44.0")]
1500 pub fn capacity(&self) -> usize {
1501 self.inner.capacity()
1504 /// Invokes [`clear`] on the underlying instance of [`OsString`].
1506 /// [`clear`]: OsString::clear
1507 #[stable(feature = "path_buf_capacity", since = "1.44.0")]
1509 pub fn clear(&mut self) {
1513 /// Invokes [`reserve`] on the underlying instance of [`OsString`].
1515 /// [`reserve`]: OsString::reserve
1516 #[stable(feature = "path_buf_capacity", since = "1.44.0")]
1518 pub fn reserve(&mut self, additional: usize) {
1519 self.inner.reserve(additional)
1522 /// Invokes [`try_reserve`] on the underlying instance of [`OsString`].
1524 /// [`try_reserve`]: OsString::try_reserve
1525 #[unstable(feature = "try_reserve_2", issue = "91789")]
1527 pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
1528 self.inner.try_reserve(additional)
1531 /// Invokes [`reserve_exact`] on the underlying instance of [`OsString`].
1533 /// [`reserve_exact`]: OsString::reserve_exact
1534 #[stable(feature = "path_buf_capacity", since = "1.44.0")]
1536 pub fn reserve_exact(&mut self, additional: usize) {
1537 self.inner.reserve_exact(additional)
1540 /// Invokes [`try_reserve_exact`] on the underlying instance of [`OsString`].
1542 /// [`try_reserve_exact`]: OsString::try_reserve_exact
1543 #[unstable(feature = "try_reserve_2", issue = "91789")]
1545 pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> {
1546 self.inner.try_reserve_exact(additional)
1549 /// Invokes [`shrink_to_fit`] on the underlying instance of [`OsString`].
1551 /// [`shrink_to_fit`]: OsString::shrink_to_fit
1552 #[stable(feature = "path_buf_capacity", since = "1.44.0")]
1554 pub fn shrink_to_fit(&mut self) {
1555 self.inner.shrink_to_fit()
1558 /// Invokes [`shrink_to`] on the underlying instance of [`OsString`].
1560 /// [`shrink_to`]: OsString::shrink_to
1561 #[stable(feature = "shrink_to", since = "1.56.0")]
1563 pub fn shrink_to(&mut self, min_capacity: usize) {
1564 self.inner.shrink_to(min_capacity)
1568 #[stable(feature = "rust1", since = "1.0.0")]
1569 impl Clone for PathBuf {
1571 fn clone(&self) -> Self {
1572 PathBuf { inner: self.inner.clone() }
1576 fn clone_from(&mut self, source: &Self) {
1577 self.inner.clone_from(&source.inner)
1581 #[stable(feature = "box_from_path", since = "1.17.0")]
1582 impl From<&Path> for Box<Path> {
1583 /// Creates a boxed [`Path`] from a reference.
1585 /// This will allocate and clone `path` to it.
1586 fn from(path: &Path) -> Box<Path> {
1587 let boxed: Box<OsStr> = path.inner.into();
1588 let rw = Box::into_raw(boxed) as *mut Path;
1589 unsafe { Box::from_raw(rw) }
1593 #[stable(feature = "box_from_cow", since = "1.45.0")]
1594 impl From<Cow<'_, Path>> for Box<Path> {
1595 /// Creates a boxed [`Path`] from a clone-on-write pointer.
1597 /// Converting from a `Cow::Owned` does not clone or allocate.
1599 fn from(cow: Cow<'_, Path>) -> Box<Path> {
1601 Cow::Borrowed(path) => Box::from(path),
1602 Cow::Owned(path) => Box::from(path),
1607 #[stable(feature = "path_buf_from_box", since = "1.18.0")]
1608 impl From<Box<Path>> for PathBuf {
1609 /// Converts a <code>[Box]<[Path]></code> into a [`PathBuf`].
1611 /// This conversion does not allocate or copy memory.
1613 fn from(boxed: Box<Path>) -> PathBuf {
1614 boxed.into_path_buf()
1618 #[stable(feature = "box_from_path_buf", since = "1.20.0")]
1619 impl From<PathBuf> for Box<Path> {
1620 /// Converts a [`PathBuf`] into a <code>[Box]<[Path]></code>.
1622 /// This conversion currently should not allocate memory,
1623 /// but this behavior is not guaranteed on all platforms or in all future versions.
1625 fn from(p: PathBuf) -> Box<Path> {
1630 #[stable(feature = "more_box_slice_clone", since = "1.29.0")]
1631 impl Clone for Box<Path> {
1633 fn clone(&self) -> Self {
1634 self.to_path_buf().into_boxed_path()
1638 #[stable(feature = "rust1", since = "1.0.0")]
1639 impl<T: ?Sized + AsRef<OsStr>> From<&T> for PathBuf {
1640 /// Converts a borrowed [`OsStr`] to a [`PathBuf`].
1642 /// Allocates a [`PathBuf`] and copies the data into it.
1644 fn from(s: &T) -> PathBuf {
1645 PathBuf::from(s.as_ref().to_os_string())
1649 #[stable(feature = "rust1", since = "1.0.0")]
1650 impl From<OsString> for PathBuf {
1651 /// Converts an [`OsString`] into a [`PathBuf`]
1653 /// This conversion does not allocate or copy memory.
1655 fn from(s: OsString) -> PathBuf {
1656 PathBuf { inner: s }
1660 #[stable(feature = "from_path_buf_for_os_string", since = "1.14.0")]
1661 impl From<PathBuf> for OsString {
1662 /// Converts a [`PathBuf`] into an [`OsString`]
1664 /// This conversion does not allocate or copy memory.
1666 fn from(path_buf: PathBuf) -> OsString {
1671 #[stable(feature = "rust1", since = "1.0.0")]
1672 impl From<String> for PathBuf {
1673 /// Converts a [`String`] into a [`PathBuf`]
1675 /// This conversion does not allocate or copy memory.
1677 fn from(s: String) -> PathBuf {
1678 PathBuf::from(OsString::from(s))
1682 #[stable(feature = "path_from_str", since = "1.32.0")]
1683 impl FromStr for PathBuf {
1684 type Err = core::convert::Infallible;
1687 fn from_str(s: &str) -> Result<Self, Self::Err> {
1688 Ok(PathBuf::from(s))
1692 #[stable(feature = "rust1", since = "1.0.0")]
1693 impl<P: AsRef<Path>> iter::FromIterator<P> for PathBuf {
1694 fn from_iter<I: IntoIterator<Item = P>>(iter: I) -> PathBuf {
1695 let mut buf = PathBuf::new();
1701 #[stable(feature = "rust1", since = "1.0.0")]
1702 impl<P: AsRef<Path>> iter::Extend<P> for PathBuf {
1703 fn extend<I: IntoIterator<Item = P>>(&mut self, iter: I) {
1704 iter.into_iter().for_each(move |p| self.push(p.as_ref()));
1708 fn extend_one(&mut self, p: P) {
1709 self.push(p.as_ref());
1713 #[stable(feature = "rust1", since = "1.0.0")]
1714 impl fmt::Debug for PathBuf {
1715 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
1716 fmt::Debug::fmt(&**self, formatter)
1720 #[stable(feature = "rust1", since = "1.0.0")]
1721 impl ops::Deref for PathBuf {
1724 fn deref(&self) -> &Path {
1725 Path::new(&self.inner)
1729 #[stable(feature = "rust1", since = "1.0.0")]
1730 impl Borrow<Path> for PathBuf {
1732 fn borrow(&self) -> &Path {
1737 #[stable(feature = "default_for_pathbuf", since = "1.17.0")]
1738 impl Default for PathBuf {
1740 fn default() -> Self {
1745 #[stable(feature = "cow_from_path", since = "1.6.0")]
1746 impl<'a> From<&'a Path> for Cow<'a, Path> {
1747 /// Creates a clone-on-write pointer from a reference to
1750 /// This conversion does not clone or allocate.
1752 fn from(s: &'a Path) -> Cow<'a, Path> {
1757 #[stable(feature = "cow_from_path", since = "1.6.0")]
1758 impl<'a> From<PathBuf> for Cow<'a, Path> {
1759 /// Creates a clone-on-write pointer from an owned
1760 /// instance of [`PathBuf`].
1762 /// This conversion does not clone or allocate.
1764 fn from(s: PathBuf) -> Cow<'a, Path> {
1769 #[stable(feature = "cow_from_pathbuf_ref", since = "1.28.0")]
1770 impl<'a> From<&'a PathBuf> for Cow<'a, Path> {
1771 /// Creates a clone-on-write pointer from a reference to
1774 /// This conversion does not clone or allocate.
1776 fn from(p: &'a PathBuf) -> Cow<'a, Path> {
1777 Cow::Borrowed(p.as_path())
1781 #[stable(feature = "pathbuf_from_cow_path", since = "1.28.0")]
1782 impl<'a> From<Cow<'a, Path>> for PathBuf {
1783 /// Converts a clone-on-write pointer to an owned path.
1785 /// Converting from a `Cow::Owned` does not clone or allocate.
1787 fn from(p: Cow<'a, Path>) -> Self {
1792 #[stable(feature = "shared_from_slice2", since = "1.24.0")]
1793 impl From<PathBuf> for Arc<Path> {
1794 /// Converts a [`PathBuf`] into an <code>[Arc]<[Path]></code> by moving the [`PathBuf`] data
1795 /// into a new [`Arc`] buffer.
1797 fn from(s: PathBuf) -> Arc<Path> {
1798 let arc: Arc<OsStr> = Arc::from(s.into_os_string());
1799 unsafe { Arc::from_raw(Arc::into_raw(arc) as *const Path) }
1803 #[stable(feature = "shared_from_slice2", since = "1.24.0")]
1804 impl From<&Path> for Arc<Path> {
1805 /// Converts a [`Path`] into an [`Arc`] by copying the [`Path`] data into a new [`Arc`] buffer.
1807 fn from(s: &Path) -> Arc<Path> {
1808 let arc: Arc<OsStr> = Arc::from(s.as_os_str());
1809 unsafe { Arc::from_raw(Arc::into_raw(arc) as *const Path) }
1813 #[stable(feature = "shared_from_slice2", since = "1.24.0")]
1814 impl From<PathBuf> for Rc<Path> {
1815 /// Converts a [`PathBuf`] into an <code>[Rc]<[Path]></code> by moving the [`PathBuf`] data into
1816 /// a new [`Rc`] buffer.
1818 fn from(s: PathBuf) -> Rc<Path> {
1819 let rc: Rc<OsStr> = Rc::from(s.into_os_string());
1820 unsafe { Rc::from_raw(Rc::into_raw(rc) as *const Path) }
1824 #[stable(feature = "shared_from_slice2", since = "1.24.0")]
1825 impl From<&Path> for Rc<Path> {
1826 /// Converts a [`Path`] into an [`Rc`] by copying the [`Path`] data into a new [`Rc`] buffer.
1828 fn from(s: &Path) -> Rc<Path> {
1829 let rc: Rc<OsStr> = Rc::from(s.as_os_str());
1830 unsafe { Rc::from_raw(Rc::into_raw(rc) as *const Path) }
1834 #[stable(feature = "rust1", since = "1.0.0")]
1835 impl ToOwned for Path {
1836 type Owned = PathBuf;
1838 fn to_owned(&self) -> PathBuf {
1842 fn clone_into(&self, target: &mut PathBuf) {
1843 self.inner.clone_into(&mut target.inner);
1847 #[stable(feature = "rust1", since = "1.0.0")]
1848 impl cmp::PartialEq for PathBuf {
1850 fn eq(&self, other: &PathBuf) -> bool {
1851 self.components() == other.components()
1855 #[stable(feature = "rust1", since = "1.0.0")]
1856 impl Hash for PathBuf {
1857 fn hash<H: Hasher>(&self, h: &mut H) {
1858 self.as_path().hash(h)
1862 #[stable(feature = "rust1", since = "1.0.0")]
1863 impl cmp::Eq for PathBuf {}
1865 #[stable(feature = "rust1", since = "1.0.0")]
1866 impl cmp::PartialOrd for PathBuf {
1868 fn partial_cmp(&self, other: &PathBuf) -> Option<cmp::Ordering> {
1869 Some(compare_components(self.components(), other.components()))
1873 #[stable(feature = "rust1", since = "1.0.0")]
1874 impl cmp::Ord for PathBuf {
1876 fn cmp(&self, other: &PathBuf) -> cmp::Ordering {
1877 compare_components(self.components(), other.components())
1881 #[stable(feature = "rust1", since = "1.0.0")]
1882 impl AsRef<OsStr> for PathBuf {
1884 fn as_ref(&self) -> &OsStr {
1889 /// A slice of a path (akin to [`str`]).
1891 /// This type supports a number of operations for inspecting a path, including
1892 /// breaking the path into its components (separated by `/` on Unix and by either
1893 /// `/` or `\` on Windows), extracting the file name, determining whether the path
1894 /// is absolute, and so on.
1896 /// This is an *unsized* type, meaning that it must always be used behind a
1897 /// pointer like `&` or [`Box`]. For an owned version of this type,
1898 /// see [`PathBuf`].
1900 /// More details about the overall approach can be found in
1901 /// the [module documentation](self).
1906 /// use std::path::Path;
1907 /// use std::ffi::OsStr;
1909 /// // Note: this example does work on Windows
1910 /// let path = Path::new("./foo/bar.txt");
1912 /// let parent = path.parent();
1913 /// assert_eq!(parent, Some(Path::new("./foo")));
1915 /// let file_stem = path.file_stem();
1916 /// assert_eq!(file_stem, Some(OsStr::new("bar")));
1918 /// let extension = path.extension();
1919 /// assert_eq!(extension, Some(OsStr::new("txt")));
1921 #[cfg_attr(not(test), rustc_diagnostic_item = "Path")]
1922 #[stable(feature = "rust1", since = "1.0.0")]
1924 // `Path::new` current implementation relies
1925 // on `Path` being layout-compatible with `OsStr`.
1926 // When attribute privacy is implemented, `Path` should be annotated as `#[repr(transparent)]`.
1927 // Anyway, `Path` representation and layout are considered implementation detail, are
1928 // not documented and must not be relied upon.
1933 /// An error returned from [`Path::strip_prefix`] if the prefix was not found.
1935 /// This `struct` is created by the [`strip_prefix`] method on [`Path`].
1936 /// See its documentation for more.
1938 /// [`strip_prefix`]: Path::strip_prefix
1939 #[derive(Debug, Clone, PartialEq, Eq)]
1940 #[stable(since = "1.7.0", feature = "strip_prefix")]
1941 pub struct StripPrefixError(());
1944 // The following (private!) function allows construction of a path from a u8
1945 // slice, which is only safe when it is known to follow the OsStr encoding.
1946 unsafe fn from_u8_slice(s: &[u8]) -> &Path {
1947 unsafe { Path::new(u8_slice_as_os_str(s)) }
1949 // The following (private!) function reveals the byte encoding used for OsStr.
1950 fn as_u8_slice(&self) -> &[u8] {
1951 os_str_as_u8_slice(&self.inner)
1954 /// Directly wraps a string slice as a `Path` slice.
1956 /// This is a cost-free conversion.
1961 /// use std::path::Path;
1963 /// Path::new("foo.txt");
1966 /// You can create `Path`s from `String`s, or even other `Path`s:
1969 /// use std::path::Path;
1971 /// let string = String::from("foo.txt");
1972 /// let from_string = Path::new(&string);
1973 /// let from_path = Path::new(&from_string);
1974 /// assert_eq!(from_string, from_path);
1976 #[stable(feature = "rust1", since = "1.0.0")]
1977 pub fn new<S: AsRef<OsStr> + ?Sized>(s: &S) -> &Path {
1978 unsafe { &*(s.as_ref() as *const OsStr as *const Path) }
1981 /// Yields the underlying [`OsStr`] slice.
1986 /// use std::path::Path;
1988 /// let os_str = Path::new("foo.txt").as_os_str();
1989 /// assert_eq!(os_str, std::ffi::OsStr::new("foo.txt"));
1991 #[stable(feature = "rust1", since = "1.0.0")]
1994 pub fn as_os_str(&self) -> &OsStr {
1998 /// Yields a [`&str`] slice if the `Path` is valid unicode.
2000 /// This conversion may entail doing a check for UTF-8 validity.
2001 /// Note that validation is performed because non-UTF-8 strings are
2002 /// perfectly valid for some OS.
2009 /// use std::path::Path;
2011 /// let path = Path::new("foo.txt");
2012 /// assert_eq!(path.to_str(), Some("foo.txt"));
2014 #[stable(feature = "rust1", since = "1.0.0")]
2015 #[must_use = "this returns the result of the operation, \
2016 without modifying the original"]
2018 pub fn to_str(&self) -> Option<&str> {
2022 /// Converts a `Path` to a [`Cow<str>`].
2024 /// Any non-Unicode sequences are replaced with
2025 /// [`U+FFFD REPLACEMENT CHARACTER`][U+FFFD].
2027 /// [U+FFFD]: super::char::REPLACEMENT_CHARACTER
2031 /// Calling `to_string_lossy` on a `Path` with valid unicode:
2034 /// use std::path::Path;
2036 /// let path = Path::new("foo.txt");
2037 /// assert_eq!(path.to_string_lossy(), "foo.txt");
2040 /// Had `path` contained invalid unicode, the `to_string_lossy` call might
2041 /// have returned `"fo�.txt"`.
2042 #[stable(feature = "rust1", since = "1.0.0")]
2043 #[must_use = "this returns the result of the operation, \
2044 without modifying the original"]
2046 pub fn to_string_lossy(&self) -> Cow<'_, str> {
2047 self.inner.to_string_lossy()
2050 /// Converts a `Path` to an owned [`PathBuf`].
2055 /// use std::path::Path;
2057 /// let path_buf = Path::new("foo.txt").to_path_buf();
2058 /// assert_eq!(path_buf, std::path::PathBuf::from("foo.txt"));
2060 #[rustc_conversion_suggestion]
2061 #[must_use = "this returns the result of the operation, \
2062 without modifying the original"]
2063 #[stable(feature = "rust1", since = "1.0.0")]
2064 pub fn to_path_buf(&self) -> PathBuf {
2065 PathBuf::from(self.inner.to_os_string())
2068 /// Returns `true` if the `Path` is absolute, i.e., if it is independent of
2069 /// the current directory.
2071 /// * On Unix, a path is absolute if it starts with the root, so
2072 /// `is_absolute` and [`has_root`] are equivalent.
2074 /// * On Windows, a path is absolute if it has a prefix and starts with the
2075 /// root: `c:\windows` is absolute, while `c:temp` and `\temp` are not.
2080 /// use std::path::Path;
2082 /// assert!(!Path::new("foo.txt").is_absolute());
2085 /// [`has_root`]: Path::has_root
2086 #[stable(feature = "rust1", since = "1.0.0")]
2088 #[allow(deprecated)]
2089 pub fn is_absolute(&self) -> bool {
2090 if cfg!(target_os = "redox") {
2091 // FIXME: Allow Redox prefixes
2092 self.has_root() || has_redox_scheme(self.as_u8_slice())
2094 self.has_root() && (cfg!(any(unix, target_os = "wasi")) || self.prefix().is_some())
2098 /// Returns `true` if the `Path` is relative, i.e., not absolute.
2100 /// See [`is_absolute`]'s documentation for more details.
2105 /// use std::path::Path;
2107 /// assert!(Path::new("foo.txt").is_relative());
2110 /// [`is_absolute`]: Path::is_absolute
2111 #[stable(feature = "rust1", since = "1.0.0")]
2114 pub fn is_relative(&self) -> bool {
2118 fn prefix(&self) -> Option<Prefix<'_>> {
2119 self.components().prefix
2122 /// Returns `true` if the `Path` has a root.
2124 /// * On Unix, a path has a root if it begins with `/`.
2126 /// * On Windows, a path has a root if it:
2127 /// * has no prefix and begins with a separator, e.g., `\windows`
2128 /// * has a prefix followed by a separator, e.g., `c:\windows` but not `c:windows`
2129 /// * has any non-disk prefix, e.g., `\\server\share`
2134 /// use std::path::Path;
2136 /// assert!(Path::new("/etc/passwd").has_root());
2138 #[stable(feature = "rust1", since = "1.0.0")]
2141 pub fn has_root(&self) -> bool {
2142 self.components().has_root()
2145 /// Returns the `Path` without its final component, if there is one.
2147 /// Returns [`None`] if the path terminates in a root or prefix.
2152 /// use std::path::Path;
2154 /// let path = Path::new("/foo/bar");
2155 /// let parent = path.parent().unwrap();
2156 /// assert_eq!(parent, Path::new("/foo"));
2158 /// let grand_parent = parent.parent().unwrap();
2159 /// assert_eq!(grand_parent, Path::new("/"));
2160 /// assert_eq!(grand_parent.parent(), None);
2162 #[stable(feature = "rust1", since = "1.0.0")]
2164 pub fn parent(&self) -> Option<&Path> {
2165 let mut comps = self.components();
2166 let comp = comps.next_back();
2167 comp.and_then(|p| match p {
2168 Component::Normal(_) | Component::CurDir | Component::ParentDir => {
2169 Some(comps.as_path())
2175 /// Produces an iterator over `Path` and its ancestors.
2177 /// The iterator will yield the `Path` that is returned if the [`parent`] method is used zero
2178 /// or more times. That means, the iterator will yield `&self`, `&self.parent().unwrap()`,
2179 /// `&self.parent().unwrap().parent().unwrap()` and so on. If the [`parent`] method returns
2180 /// [`None`], the iterator will do likewise. The iterator will always yield at least one value,
2186 /// use std::path::Path;
2188 /// let mut ancestors = Path::new("/foo/bar").ancestors();
2189 /// assert_eq!(ancestors.next(), Some(Path::new("/foo/bar")));
2190 /// assert_eq!(ancestors.next(), Some(Path::new("/foo")));
2191 /// assert_eq!(ancestors.next(), Some(Path::new("/")));
2192 /// assert_eq!(ancestors.next(), None);
2194 /// let mut ancestors = Path::new("../foo/bar").ancestors();
2195 /// assert_eq!(ancestors.next(), Some(Path::new("../foo/bar")));
2196 /// assert_eq!(ancestors.next(), Some(Path::new("../foo")));
2197 /// assert_eq!(ancestors.next(), Some(Path::new("..")));
2198 /// assert_eq!(ancestors.next(), Some(Path::new("")));
2199 /// assert_eq!(ancestors.next(), None);
2202 /// [`parent`]: Path::parent
2203 #[stable(feature = "path_ancestors", since = "1.28.0")]
2205 pub fn ancestors(&self) -> Ancestors<'_> {
2206 Ancestors { next: Some(&self) }
2209 /// Returns the final component of the `Path`, if there is one.
2211 /// If the path is a normal file, this is the file name. If it's the path of a directory, this
2212 /// is the directory name.
2214 /// Returns [`None`] if the path terminates in `..`.
2219 /// use std::path::Path;
2220 /// use std::ffi::OsStr;
2222 /// assert_eq!(Some(OsStr::new("bin")), Path::new("/usr/bin/").file_name());
2223 /// assert_eq!(Some(OsStr::new("foo.txt")), Path::new("tmp/foo.txt").file_name());
2224 /// assert_eq!(Some(OsStr::new("foo.txt")), Path::new("foo.txt/.").file_name());
2225 /// assert_eq!(Some(OsStr::new("foo.txt")), Path::new("foo.txt/.//").file_name());
2226 /// assert_eq!(None, Path::new("foo.txt/..").file_name());
2227 /// assert_eq!(None, Path::new("/").file_name());
2229 #[stable(feature = "rust1", since = "1.0.0")]
2231 pub fn file_name(&self) -> Option<&OsStr> {
2232 self.components().next_back().and_then(|p| match p {
2233 Component::Normal(p) => Some(p),
2238 /// Returns a path that, when joined onto `base`, yields `self`.
2242 /// If `base` is not a prefix of `self` (i.e., [`starts_with`]
2243 /// returns `false`), returns [`Err`].
2245 /// [`starts_with`]: Path::starts_with
2250 /// use std::path::{Path, PathBuf};
2252 /// let path = Path::new("/test/haha/foo.txt");
2254 /// assert_eq!(path.strip_prefix("/"), Ok(Path::new("test/haha/foo.txt")));
2255 /// assert_eq!(path.strip_prefix("/test"), Ok(Path::new("haha/foo.txt")));
2256 /// assert_eq!(path.strip_prefix("/test/"), Ok(Path::new("haha/foo.txt")));
2257 /// assert_eq!(path.strip_prefix("/test/haha/foo.txt"), Ok(Path::new("")));
2258 /// assert_eq!(path.strip_prefix("/test/haha/foo.txt/"), Ok(Path::new("")));
2260 /// assert!(path.strip_prefix("test").is_err());
2261 /// assert!(path.strip_prefix("/haha").is_err());
2263 /// let prefix = PathBuf::from("/test/");
2264 /// assert_eq!(path.strip_prefix(prefix), Ok(Path::new("haha/foo.txt")));
2266 #[stable(since = "1.7.0", feature = "path_strip_prefix")]
2267 pub fn strip_prefix<P>(&self, base: P) -> Result<&Path, StripPrefixError>
2271 self._strip_prefix(base.as_ref())
2274 fn _strip_prefix(&self, base: &Path) -> Result<&Path, StripPrefixError> {
2275 iter_after(self.components(), base.components())
2276 .map(|c| c.as_path())
2277 .ok_or(StripPrefixError(()))
2280 /// Determines whether `base` is a prefix of `self`.
2282 /// Only considers whole path components to match.
2287 /// use std::path::Path;
2289 /// let path = Path::new("/etc/passwd");
2291 /// assert!(path.starts_with("/etc"));
2292 /// assert!(path.starts_with("/etc/"));
2293 /// assert!(path.starts_with("/etc/passwd"));
2294 /// assert!(path.starts_with("/etc/passwd/")); // extra slash is okay
2295 /// assert!(path.starts_with("/etc/passwd///")); // multiple extra slashes are okay
2297 /// assert!(!path.starts_with("/e"));
2298 /// assert!(!path.starts_with("/etc/passwd.txt"));
2300 /// assert!(!Path::new("/etc/foo.rs").starts_with("/etc/foo"));
2302 #[stable(feature = "rust1", since = "1.0.0")]
2304 pub fn starts_with<P: AsRef<Path>>(&self, base: P) -> bool {
2305 self._starts_with(base.as_ref())
2308 fn _starts_with(&self, base: &Path) -> bool {
2309 iter_after(self.components(), base.components()).is_some()
2312 /// Determines whether `child` is a suffix of `self`.
2314 /// Only considers whole path components to match.
2319 /// use std::path::Path;
2321 /// let path = Path::new("/etc/resolv.conf");
2323 /// assert!(path.ends_with("resolv.conf"));
2324 /// assert!(path.ends_with("etc/resolv.conf"));
2325 /// assert!(path.ends_with("/etc/resolv.conf"));
2327 /// assert!(!path.ends_with("/resolv.conf"));
2328 /// assert!(!path.ends_with("conf")); // use .extension() instead
2330 #[stable(feature = "rust1", since = "1.0.0")]
2332 pub fn ends_with<P: AsRef<Path>>(&self, child: P) -> bool {
2333 self._ends_with(child.as_ref())
2336 fn _ends_with(&self, child: &Path) -> bool {
2337 iter_after(self.components().rev(), child.components().rev()).is_some()
2340 /// Extracts the stem (non-extension) portion of [`self.file_name`].
2342 /// [`self.file_name`]: Path::file_name
2346 /// * [`None`], if there is no file name;
2347 /// * The entire file name if there is no embedded `.`;
2348 /// * The entire file name if the file name begins with `.` and has no other `.`s within;
2349 /// * Otherwise, the portion of the file name before the final `.`
2354 /// use std::path::Path;
2356 /// assert_eq!("foo", Path::new("foo.rs").file_stem().unwrap());
2357 /// assert_eq!("foo.tar", Path::new("foo.tar.gz").file_stem().unwrap());
2361 /// This method is similar to [`Path::file_prefix`], which extracts the portion of the file name
2362 /// before the *first* `.`
2364 /// [`Path::file_prefix`]: Path::file_prefix
2366 #[stable(feature = "rust1", since = "1.0.0")]
2368 pub fn file_stem(&self) -> Option<&OsStr> {
2369 self.file_name().map(rsplit_file_at_dot).and_then(|(before, after)| before.or(after))
2372 /// Extracts the prefix of [`self.file_name`].
2376 /// * [`None`], if there is no file name;
2377 /// * The entire file name if there is no embedded `.`;
2378 /// * The portion of the file name before the first non-beginning `.`;
2379 /// * The entire file name if the file name begins with `.` and has no other `.`s within;
2380 /// * The portion of the file name before the second `.` if the file name begins with `.`
2382 /// [`self.file_name`]: Path::file_name
2387 /// # #![feature(path_file_prefix)]
2388 /// use std::path::Path;
2390 /// assert_eq!("foo", Path::new("foo.rs").file_prefix().unwrap());
2391 /// assert_eq!("foo", Path::new("foo.tar.gz").file_prefix().unwrap());
2395 /// This method is similar to [`Path::file_stem`], which extracts the portion of the file name
2396 /// before the *last* `.`
2398 /// [`Path::file_stem`]: Path::file_stem
2400 #[unstable(feature = "path_file_prefix", issue = "86319")]
2402 pub fn file_prefix(&self) -> Option<&OsStr> {
2403 self.file_name().map(split_file_at_dot).and_then(|(before, _after)| Some(before))
2406 /// Extracts the extension of [`self.file_name`], if possible.
2408 /// The extension is:
2410 /// * [`None`], if there is no file name;
2411 /// * [`None`], if there is no embedded `.`;
2412 /// * [`None`], if the file name begins with `.` and has no other `.`s within;
2413 /// * Otherwise, the portion of the file name after the final `.`
2415 /// [`self.file_name`]: Path::file_name
2420 /// use std::path::Path;
2422 /// assert_eq!("rs", Path::new("foo.rs").extension().unwrap());
2423 /// assert_eq!("gz", Path::new("foo.tar.gz").extension().unwrap());
2425 #[stable(feature = "rust1", since = "1.0.0")]
2427 pub fn extension(&self) -> Option<&OsStr> {
2428 self.file_name().map(rsplit_file_at_dot).and_then(|(before, after)| before.and(after))
2431 /// Creates an owned [`PathBuf`] with `path` adjoined to `self`.
2433 /// See [`PathBuf::push`] for more details on what it means to adjoin a path.
2438 /// use std::path::{Path, PathBuf};
2440 /// assert_eq!(Path::new("/etc").join("passwd"), PathBuf::from("/etc/passwd"));
2442 #[stable(feature = "rust1", since = "1.0.0")]
2444 pub fn join<P: AsRef<Path>>(&self, path: P) -> PathBuf {
2445 self._join(path.as_ref())
2448 fn _join(&self, path: &Path) -> PathBuf {
2449 let mut buf = self.to_path_buf();
2454 /// Creates an owned [`PathBuf`] like `self` but with the given file name.
2456 /// See [`PathBuf::set_file_name`] for more details.
2461 /// use std::path::{Path, PathBuf};
2463 /// let path = Path::new("/tmp/foo.txt");
2464 /// assert_eq!(path.with_file_name("bar.txt"), PathBuf::from("/tmp/bar.txt"));
2466 /// let path = Path::new("/tmp");
2467 /// assert_eq!(path.with_file_name("var"), PathBuf::from("/var"));
2469 #[stable(feature = "rust1", since = "1.0.0")]
2471 pub fn with_file_name<S: AsRef<OsStr>>(&self, file_name: S) -> PathBuf {
2472 self._with_file_name(file_name.as_ref())
2475 fn _with_file_name(&self, file_name: &OsStr) -> PathBuf {
2476 let mut buf = self.to_path_buf();
2477 buf.set_file_name(file_name);
2481 /// Creates an owned [`PathBuf`] like `self` but with the given extension.
2483 /// See [`PathBuf::set_extension`] for more details.
2488 /// use std::path::{Path, PathBuf};
2490 /// let path = Path::new("foo.rs");
2491 /// assert_eq!(path.with_extension("txt"), PathBuf::from("foo.txt"));
2493 /// let path = Path::new("foo.tar.gz");
2494 /// assert_eq!(path.with_extension(""), PathBuf::from("foo.tar"));
2495 /// assert_eq!(path.with_extension("xz"), PathBuf::from("foo.tar.xz"));
2496 /// assert_eq!(path.with_extension("").with_extension("txt"), PathBuf::from("foo.txt"));
2498 #[stable(feature = "rust1", since = "1.0.0")]
2499 pub fn with_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf {
2500 self._with_extension(extension.as_ref())
2503 fn _with_extension(&self, extension: &OsStr) -> PathBuf {
2504 let mut buf = self.to_path_buf();
2505 buf.set_extension(extension);
2509 /// Produces an iterator over the [`Component`]s of the path.
2511 /// When parsing the path, there is a small amount of normalization:
2513 /// * Repeated separators are ignored, so `a/b` and `a//b` both have
2514 /// `a` and `b` as components.
2516 /// * Occurrences of `.` are normalized away, except if they are at the
2517 /// beginning of the path. For example, `a/./b`, `a/b/`, `a/b/.` and
2518 /// `a/b` all have `a` and `b` as components, but `./a/b` starts with
2519 /// an additional [`CurDir`] component.
2521 /// * A trailing slash is normalized away, `/a/b` and `/a/b/` are equivalent.
2523 /// Note that no other normalization takes place; in particular, `a/c`
2524 /// and `a/b/../c` are distinct, to account for the possibility that `b`
2525 /// is a symbolic link (so its parent isn't `a`).
2530 /// use std::path::{Path, Component};
2531 /// use std::ffi::OsStr;
2533 /// let mut components = Path::new("/tmp/foo.txt").components();
2535 /// assert_eq!(components.next(), Some(Component::RootDir));
2536 /// assert_eq!(components.next(), Some(Component::Normal(OsStr::new("tmp"))));
2537 /// assert_eq!(components.next(), Some(Component::Normal(OsStr::new("foo.txt"))));
2538 /// assert_eq!(components.next(), None)
2541 /// [`CurDir`]: Component::CurDir
2542 #[stable(feature = "rust1", since = "1.0.0")]
2543 pub fn components(&self) -> Components<'_> {
2544 let prefix = parse_prefix(self.as_os_str());
2546 path: self.as_u8_slice(),
2548 has_physical_root: has_physical_root(self.as_u8_slice(), prefix)
2549 || has_redox_scheme(self.as_u8_slice()),
2550 front: State::Prefix,
2555 /// Produces an iterator over the path's components viewed as [`OsStr`]
2558 /// For more information about the particulars of how the path is separated
2559 /// into components, see [`components`].
2561 /// [`components`]: Path::components
2566 /// use std::path::{self, Path};
2567 /// use std::ffi::OsStr;
2569 /// let mut it = Path::new("/tmp/foo.txt").iter();
2570 /// assert_eq!(it.next(), Some(OsStr::new(&path::MAIN_SEPARATOR.to_string())));
2571 /// assert_eq!(it.next(), Some(OsStr::new("tmp")));
2572 /// assert_eq!(it.next(), Some(OsStr::new("foo.txt")));
2573 /// assert_eq!(it.next(), None)
2575 #[stable(feature = "rust1", since = "1.0.0")]
2577 pub fn iter(&self) -> Iter<'_> {
2578 Iter { inner: self.components() }
2581 /// Returns an object that implements [`Display`] for safely printing paths
2582 /// that may contain non-Unicode data. This may perform lossy conversion,
2583 /// depending on the platform. If you would like an implementation which
2584 /// escapes the path please use [`Debug`] instead.
2586 /// [`Display`]: fmt::Display
2591 /// use std::path::Path;
2593 /// let path = Path::new("/tmp/foo.rs");
2595 /// println!("{}", path.display());
2597 #[stable(feature = "rust1", since = "1.0.0")]
2598 #[must_use = "this does not display the path, \
2599 it returns an object that can be displayed"]
2601 pub fn display(&self) -> Display<'_> {
2602 Display { path: self }
2605 /// Queries the file system to get information about a file, directory, etc.
2607 /// This function will traverse symbolic links to query information about the
2608 /// destination file.
2610 /// This is an alias to [`fs::metadata`].
2615 /// use std::path::Path;
2617 /// let path = Path::new("/Minas/tirith");
2618 /// let metadata = path.metadata().expect("metadata call failed");
2619 /// println!("{:?}", metadata.file_type());
2621 #[stable(feature = "path_ext", since = "1.5.0")]
2623 pub fn metadata(&self) -> io::Result<fs::Metadata> {
2627 /// Queries the metadata about a file without following symlinks.
2629 /// This is an alias to [`fs::symlink_metadata`].
2634 /// use std::path::Path;
2636 /// let path = Path::new("/Minas/tirith");
2637 /// let metadata = path.symlink_metadata().expect("symlink_metadata call failed");
2638 /// println!("{:?}", metadata.file_type());
2640 #[stable(feature = "path_ext", since = "1.5.0")]
2642 pub fn symlink_metadata(&self) -> io::Result<fs::Metadata> {
2643 fs::symlink_metadata(self)
2646 /// Returns the canonical, absolute form of the path with all intermediate
2647 /// components normalized and symbolic links resolved.
2649 /// This is an alias to [`fs::canonicalize`].
2654 /// use std::path::{Path, PathBuf};
2656 /// let path = Path::new("/foo/test/../test/bar.rs");
2657 /// assert_eq!(path.canonicalize().unwrap(), PathBuf::from("/foo/test/bar.rs"));
2659 #[stable(feature = "path_ext", since = "1.5.0")]
2661 pub fn canonicalize(&self) -> io::Result<PathBuf> {
2662 fs::canonicalize(self)
2665 /// Reads a symbolic link, returning the file that the link points to.
2667 /// This is an alias to [`fs::read_link`].
2672 /// use std::path::Path;
2674 /// let path = Path::new("/laputa/sky_castle.rs");
2675 /// let path_link = path.read_link().expect("read_link call failed");
2677 #[stable(feature = "path_ext", since = "1.5.0")]
2679 pub fn read_link(&self) -> io::Result<PathBuf> {
2683 /// Returns an iterator over the entries within a directory.
2685 /// The iterator will yield instances of <code>[io::Result]<[fs::DirEntry]></code>. New
2686 /// errors may be encountered after an iterator is initially constructed.
2688 /// This is an alias to [`fs::read_dir`].
2693 /// use std::path::Path;
2695 /// let path = Path::new("/laputa");
2696 /// for entry in path.read_dir().expect("read_dir call failed") {
2697 /// if let Ok(entry) = entry {
2698 /// println!("{:?}", entry.path());
2702 #[stable(feature = "path_ext", since = "1.5.0")]
2704 pub fn read_dir(&self) -> io::Result<fs::ReadDir> {
2708 /// Returns `true` if the path points at an existing entity.
2710 /// This function will traverse symbolic links to query information about the
2711 /// destination file.
2713 /// If you cannot access the metadata of the file, e.g. because of a
2714 /// permission error or broken symbolic links, this will return `false`.
2719 /// use std::path::Path;
2720 /// assert!(!Path::new("does_not_exist.txt").exists());
2725 /// This is a convenience function that coerces errors to false. If you want to
2726 /// check errors, call [`fs::metadata`].
2727 #[stable(feature = "path_ext", since = "1.5.0")]
2730 pub fn exists(&self) -> bool {
2731 fs::metadata(self).is_ok()
2734 /// Returns `Ok(true)` if the path points at an existing entity.
2736 /// This function will traverse symbolic links to query information about the
2737 /// destination file. In case of broken symbolic links this will return `Ok(false)`.
2739 /// As opposed to the [`exists()`] method, this one doesn't silently ignore errors
2740 /// unrelated to the path not existing. (E.g. it will return `Err(_)` in case of permission
2741 /// denied on some of the parent directories.)
2746 /// #![feature(path_try_exists)]
2748 /// use std::path::Path;
2749 /// assert!(!Path::new("does_not_exist.txt").try_exists().expect("Can't check existence of file does_not_exist.txt"));
2750 /// assert!(Path::new("/root/secret_file.txt").try_exists().is_err());
2753 /// [`exists()`]: Self::exists
2754 // FIXME: stabilization should modify documentation of `exists()` to recommend this method
2756 #[unstable(feature = "path_try_exists", issue = "83186")]
2758 pub fn try_exists(&self) -> io::Result<bool> {
2759 fs::try_exists(self)
2762 /// Returns `true` if the path exists on disk and is pointing at a regular file.
2764 /// This function will traverse symbolic links to query information about the
2765 /// destination file.
2767 /// If you cannot access the metadata of the file, e.g. because of a
2768 /// permission error or broken symbolic links, this will return `false`.
2773 /// use std::path::Path;
2774 /// assert_eq!(Path::new("./is_a_directory/").is_file(), false);
2775 /// assert_eq!(Path::new("a_file.txt").is_file(), true);
2780 /// This is a convenience function that coerces errors to false. If you want to
2781 /// check errors, call [`fs::metadata`] and handle its [`Result`]. Then call
2782 /// [`fs::Metadata::is_file`] if it was [`Ok`].
2784 /// When the goal is simply to read from (or write to) the source, the most
2785 /// reliable way to test the source can be read (or written to) is to open
2786 /// it. Only using `is_file` can break workflows like `diff <( prog_a )` on
2787 /// a Unix-like system for example. See [`fs::File::open`] or
2788 /// [`fs::OpenOptions::open`] for more information.
2789 #[stable(feature = "path_ext", since = "1.5.0")]
2791 pub fn is_file(&self) -> bool {
2792 fs::metadata(self).map(|m| m.is_file()).unwrap_or(false)
2795 /// Returns `true` if the path exists on disk and is pointing at a directory.
2797 /// This function will traverse symbolic links to query information about the
2798 /// destination file.
2800 /// If you cannot access the metadata of the file, e.g. because of a
2801 /// permission error or broken symbolic links, this will return `false`.
2806 /// use std::path::Path;
2807 /// assert_eq!(Path::new("./is_a_directory/").is_dir(), true);
2808 /// assert_eq!(Path::new("a_file.txt").is_dir(), false);
2813 /// This is a convenience function that coerces errors to false. If you want to
2814 /// check errors, call [`fs::metadata`] and handle its [`Result`]. Then call
2815 /// [`fs::Metadata::is_dir`] if it was [`Ok`].
2816 #[stable(feature = "path_ext", since = "1.5.0")]
2818 pub fn is_dir(&self) -> bool {
2819 fs::metadata(self).map(|m| m.is_dir()).unwrap_or(false)
2822 /// Returns `true` if the path exists on disk and is pointing at a symbolic link.
2824 /// This function will not traverse symbolic links.
2825 /// In case of a broken symbolic link this will also return true.
2827 /// If you cannot access the directory containing the file, e.g., because of a
2828 /// permission error, this will return false.
2832 #[cfg_attr(unix, doc = "```no_run")]
2833 #[cfg_attr(not(unix), doc = "```ignore")]
2834 /// use std::path::Path;
2835 /// use std::os::unix::fs::symlink;
2837 /// let link_path = Path::new("link");
2838 /// symlink("/origin_does_not_exist/", link_path).unwrap();
2839 /// assert_eq!(link_path.is_symlink(), true);
2840 /// assert_eq!(link_path.exists(), false);
2845 /// This is a convenience function that coerces errors to false. If you want to
2846 /// check errors, call [`fs::symlink_metadata`] and handle its [`Result`]. Then call
2847 /// [`fs::Metadata::is_symlink`] if it was [`Ok`].
2849 #[stable(feature = "is_symlink", since = "1.58.0")]
2850 pub fn is_symlink(&self) -> bool {
2851 fs::symlink_metadata(self).map(|m| m.is_symlink()).unwrap_or(false)
2854 /// Converts a [`Box<Path>`](Box) into a [`PathBuf`] without copying or
2856 #[stable(feature = "into_boxed_path", since = "1.20.0")]
2857 #[must_use = "`self` will be dropped if the result is not used"]
2858 pub fn into_path_buf(self: Box<Path>) -> PathBuf {
2859 let rw = Box::into_raw(self) as *mut OsStr;
2860 let inner = unsafe { Box::from_raw(rw) };
2861 PathBuf { inner: OsString::from(inner) }
2865 #[stable(feature = "rust1", since = "1.0.0")]
2866 impl AsRef<OsStr> for Path {
2868 fn as_ref(&self) -> &OsStr {
2873 #[stable(feature = "rust1", since = "1.0.0")]
2874 impl fmt::Debug for Path {
2875 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
2876 fmt::Debug::fmt(&self.inner, formatter)
2880 /// Helper struct for safely printing paths with [`format!`] and `{}`.
2882 /// A [`Path`] might contain non-Unicode data. This `struct` implements the
2883 /// [`Display`] trait in a way that mitigates that. It is created by the
2884 /// [`display`](Path::display) method on [`Path`]. This may perform lossy
2885 /// conversion, depending on the platform. If you would like an implementation
2886 /// which escapes the path please use [`Debug`] instead.
2891 /// use std::path::Path;
2893 /// let path = Path::new("/tmp/foo.rs");
2895 /// println!("{}", path.display());
2898 /// [`Display`]: fmt::Display
2899 /// [`format!`]: crate::format
2900 #[stable(feature = "rust1", since = "1.0.0")]
2901 pub struct Display<'a> {
2905 #[stable(feature = "rust1", since = "1.0.0")]
2906 impl fmt::Debug for Display<'_> {
2907 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2908 fmt::Debug::fmt(&self.path, f)
2912 #[stable(feature = "rust1", since = "1.0.0")]
2913 impl fmt::Display for Display<'_> {
2914 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2915 self.path.inner.display(f)
2919 #[stable(feature = "rust1", since = "1.0.0")]
2920 impl cmp::PartialEq for Path {
2922 fn eq(&self, other: &Path) -> bool {
2923 self.components() == other.components()
2927 #[stable(feature = "rust1", since = "1.0.0")]
2928 impl Hash for Path {
2929 fn hash<H: Hasher>(&self, h: &mut H) {
2930 let bytes = self.as_u8_slice();
2931 let (prefix_len, verbatim) = match parse_prefix(&self.inner) {
2934 (prefix.len(), prefix.is_verbatim())
2938 let bytes = &bytes[prefix_len..];
2940 let mut component_start = 0;
2941 let mut bytes_hashed = 0;
2943 for i in 0..bytes.len() {
2944 let is_sep = if verbatim { is_verbatim_sep(bytes[i]) } else { is_sep_byte(bytes[i]) };
2946 if i > component_start {
2947 let to_hash = &bytes[component_start..i];
2949 bytes_hashed += to_hash.len();
2952 // skip over separator and optionally a following CurDir item
2953 // since components() would normalize these away.
2954 component_start = i + 1;
2956 let tail = &bytes[component_start..];
2959 component_start += match tail {
2961 [b'.', sep @ _, ..] if is_sep_byte(*sep) => 1,
2968 if component_start < bytes.len() {
2969 let to_hash = &bytes[component_start..];
2971 bytes_hashed += to_hash.len();
2974 h.write_usize(bytes_hashed);
2978 #[stable(feature = "rust1", since = "1.0.0")]
2979 impl cmp::Eq for Path {}
2981 #[stable(feature = "rust1", since = "1.0.0")]
2982 impl cmp::PartialOrd for Path {
2984 fn partial_cmp(&self, other: &Path) -> Option<cmp::Ordering> {
2985 Some(compare_components(self.components(), other.components()))
2989 #[stable(feature = "rust1", since = "1.0.0")]
2990 impl cmp::Ord for Path {
2992 fn cmp(&self, other: &Path) -> cmp::Ordering {
2993 compare_components(self.components(), other.components())
2997 #[stable(feature = "rust1", since = "1.0.0")]
2998 impl AsRef<Path> for Path {
3000 fn as_ref(&self) -> &Path {
3005 #[stable(feature = "rust1", since = "1.0.0")]
3006 impl AsRef<Path> for OsStr {
3008 fn as_ref(&self) -> &Path {
3013 #[stable(feature = "cow_os_str_as_ref_path", since = "1.8.0")]
3014 impl AsRef<Path> for Cow<'_, OsStr> {
3016 fn as_ref(&self) -> &Path {
3021 #[stable(feature = "rust1", since = "1.0.0")]
3022 impl AsRef<Path> for OsString {
3024 fn as_ref(&self) -> &Path {
3029 #[stable(feature = "rust1", since = "1.0.0")]
3030 impl AsRef<Path> for str {
3032 fn as_ref(&self) -> &Path {
3037 #[stable(feature = "rust1", since = "1.0.0")]
3038 impl AsRef<Path> for String {
3040 fn as_ref(&self) -> &Path {
3045 #[stable(feature = "rust1", since = "1.0.0")]
3046 impl AsRef<Path> for PathBuf {
3048 fn as_ref(&self) -> &Path {
3053 #[stable(feature = "path_into_iter", since = "1.6.0")]
3054 impl<'a> IntoIterator for &'a PathBuf {
3055 type Item = &'a OsStr;
3056 type IntoIter = Iter<'a>;
3058 fn into_iter(self) -> Iter<'a> {
3063 #[stable(feature = "path_into_iter", since = "1.6.0")]
3064 impl<'a> IntoIterator for &'a Path {
3065 type Item = &'a OsStr;
3066 type IntoIter = Iter<'a>;
3068 fn into_iter(self) -> Iter<'a> {
3073 macro_rules! impl_cmp {
3074 ($lhs:ty, $rhs: ty) => {
3075 #[stable(feature = "partialeq_path", since = "1.6.0")]
3076 impl<'a, 'b> PartialEq<$rhs> for $lhs {
3078 fn eq(&self, other: &$rhs) -> bool {
3079 <Path as PartialEq>::eq(self, other)
3083 #[stable(feature = "partialeq_path", since = "1.6.0")]
3084 impl<'a, 'b> PartialEq<$lhs> for $rhs {
3086 fn eq(&self, other: &$lhs) -> bool {
3087 <Path as PartialEq>::eq(self, other)
3091 #[stable(feature = "cmp_path", since = "1.8.0")]
3092 impl<'a, 'b> PartialOrd<$rhs> for $lhs {
3094 fn partial_cmp(&self, other: &$rhs) -> Option<cmp::Ordering> {
3095 <Path as PartialOrd>::partial_cmp(self, other)
3099 #[stable(feature = "cmp_path", since = "1.8.0")]
3100 impl<'a, 'b> PartialOrd<$lhs> for $rhs {
3102 fn partial_cmp(&self, other: &$lhs) -> Option<cmp::Ordering> {
3103 <Path as PartialOrd>::partial_cmp(self, other)
3109 impl_cmp!(PathBuf, Path);
3110 impl_cmp!(PathBuf, &'a Path);
3111 impl_cmp!(Cow<'a, Path>, Path);
3112 impl_cmp!(Cow<'a, Path>, &'b Path);
3113 impl_cmp!(Cow<'a, Path>, PathBuf);
3115 macro_rules! impl_cmp_os_str {
3116 ($lhs:ty, $rhs: ty) => {
3117 #[stable(feature = "cmp_path", since = "1.8.0")]
3118 impl<'a, 'b> PartialEq<$rhs> for $lhs {
3120 fn eq(&self, other: &$rhs) -> bool {
3121 <Path as PartialEq>::eq(self, other.as_ref())
3125 #[stable(feature = "cmp_path", since = "1.8.0")]
3126 impl<'a, 'b> PartialEq<$lhs> for $rhs {
3128 fn eq(&self, other: &$lhs) -> bool {
3129 <Path as PartialEq>::eq(self.as_ref(), other)
3133 #[stable(feature = "cmp_path", since = "1.8.0")]
3134 impl<'a, 'b> PartialOrd<$rhs> for $lhs {
3136 fn partial_cmp(&self, other: &$rhs) -> Option<cmp::Ordering> {
3137 <Path as PartialOrd>::partial_cmp(self, other.as_ref())
3141 #[stable(feature = "cmp_path", since = "1.8.0")]
3142 impl<'a, 'b> PartialOrd<$lhs> for $rhs {
3144 fn partial_cmp(&self, other: &$lhs) -> Option<cmp::Ordering> {
3145 <Path as PartialOrd>::partial_cmp(self.as_ref(), other)
3151 impl_cmp_os_str!(PathBuf, OsStr);
3152 impl_cmp_os_str!(PathBuf, &'a OsStr);
3153 impl_cmp_os_str!(PathBuf, Cow<'a, OsStr>);
3154 impl_cmp_os_str!(PathBuf, OsString);
3155 impl_cmp_os_str!(Path, OsStr);
3156 impl_cmp_os_str!(Path, &'a OsStr);
3157 impl_cmp_os_str!(Path, Cow<'a, OsStr>);
3158 impl_cmp_os_str!(Path, OsString);
3159 impl_cmp_os_str!(&'a Path, OsStr);
3160 impl_cmp_os_str!(&'a Path, Cow<'b, OsStr>);
3161 impl_cmp_os_str!(&'a Path, OsString);
3162 impl_cmp_os_str!(Cow<'a, Path>, OsStr);
3163 impl_cmp_os_str!(Cow<'a, Path>, &'b OsStr);
3164 impl_cmp_os_str!(Cow<'a, Path>, OsString);
3166 #[stable(since = "1.7.0", feature = "strip_prefix")]
3167 impl fmt::Display for StripPrefixError {
3168 #[allow(deprecated, deprecated_in_future)]
3169 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3170 self.description().fmt(f)
3174 #[stable(since = "1.7.0", feature = "strip_prefix")]
3175 impl Error for StripPrefixError {
3176 #[allow(deprecated)]
3177 fn description(&self) -> &str {
3182 /// Makes the path absolute without accessing the filesystem.
3184 /// If the path is relative, the current directory is used as the base directory.
3185 /// All intermediate components will be resolved according to platforms-specific
3186 /// rules but unlike [`canonicalize`][crate::fs::canonicalize] this does not
3187 /// resolve symlinks and may succeed even if the path does not exist.
3189 /// If the `path` is empty or getting the
3190 /// [current directory][crate::env::current_dir] fails then an error will be
3198 /// #![feature(absolute_path)]
3200 /// fn main() -> std::io::Result<()> {
3201 /// use std::path::{self, Path};
3203 /// // Relative to absolute
3204 /// let absolute = path::absolute("foo/./bar")?;
3205 /// assert!(absolute.ends_with("foo/bar"));
3207 /// // Absolute to absolute
3208 /// let absolute = path::absolute("/foo//test/.././bar.rs")?;
3209 /// assert_eq!(absolute, Path::new("/foo/test/../bar.rs"));
3212 /// # #[cfg(not(unix))]
3216 /// The path is resolved using [POSIX semantics][posix-semantics] except that
3217 /// it stops short of resolving symlinks. This means it will keep `..`
3218 /// components and trailing slashes.
3220 /// ## Windows paths
3223 /// #![feature(absolute_path)]
3224 /// # #[cfg(windows)]
3225 /// fn main() -> std::io::Result<()> {
3226 /// use std::path::{self, Path};
3228 /// // Relative to absolute
3229 /// let absolute = path::absolute("foo/./bar")?;
3230 /// assert!(absolute.ends_with(r"foo\bar"));
3232 /// // Absolute to absolute
3233 /// let absolute = path::absolute(r"C:\foo//test\..\./bar.rs")?;
3235 /// assert_eq!(absolute, Path::new(r"C:\foo\bar.rs"));
3238 /// # #[cfg(not(windows))]
3242 /// For verbatim paths this will simply return the path as given. For other
3243 /// paths this is currently equivalent to calling [`GetFullPathNameW`][windows-path]
3244 /// This may change in the future.
3246 /// [posix-semantics]: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13
3247 /// [windows-path]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfullpathnamew
3248 #[unstable(feature = "absolute_path", issue = "92750")]
3249 pub fn absolute<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
3250 let path = path.as_ref();
3251 if path.as_os_str().is_empty() {
3252 Err(io::const_io_error!(io::ErrorKind::InvalidInput, "cannot make an empty path absolute",))
3254 sys::path::absolute(path)