Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document Path::parent behavior around relative paths #104300

Merged
merged 1 commit into from
Nov 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion library/std/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2142,7 +2142,10 @@ impl Path {

/// 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
///
Expand All @@ -2156,6 +2159,14 @@ impl Path {
/// 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")]
Expand Down