X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=library%2Fstd%2Fsrc%2Fpath.rs;h=af88b9070c189dc31033414d49f6e69753a6d1dc;hb=fd3bfb35511cbcff59ce1454d3db627b576d7e92;hp=9d63281627d667be5eb94f212e649a9bfe26703e;hpb=5b82ea74b705799665b5a676b162f30d26f5108c;p=rust.git diff --git a/library/std/src/path.rs b/library/std/src/path.rs index 9d63281627d..af88b9070c1 100644 --- a/library/std/src/path.rs +++ b/library/std/src/path.rs @@ -2142,7 +2142,10 @@ pub fn has_root(&self) -> bool { /// Returns the `Path` without its final component, if there is one. /// - /// Returns [`None`] if the path terminates in a root or prefix. + /// This means it returns `Some("")` for relative paths with one component. + /// + /// Returns [`None`] if the path terminates in a root or prefix, or if it's + /// the empty string. /// /// # Examples /// @@ -2156,6 +2159,14 @@ pub fn has_root(&self) -> bool { /// let grand_parent = parent.parent().unwrap(); /// assert_eq!(grand_parent, Path::new("/")); /// assert_eq!(grand_parent.parent(), None); + /// + /// let relative_path = Path::new("foo/bar"); + /// let parent = relative_path.parent(); + /// assert_eq!(parent, Some(Path::new("foo"))); + /// let grand_parent = parent.and_then(Path::parent); + /// assert_eq!(grand_parent, Some(Path::new(""))); + /// let great_grand_parent = grand_parent.and_then(Path::parent); + /// assert_eq!(great_grand_parent, None); /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[doc(alias = "dirname")]