Skip to content

Add std::path::Path::is_empty. #31877

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

Closed
wants to merge 1 commit into from
Closed
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
29 changes: 29 additions & 0 deletions src/libstd/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1921,6 +1921,24 @@ impl Path {
pub fn is_dir(&self) -> bool {
fs::metadata(self).map(|m| m.is_dir()).unwrap_or(false)
}

/// Returns true if the path is empty.
///
/// # Examples
///
/// ```
/// use std::path::Path;
///
/// let mut path = Path::new("");
/// assert!(path.is_empty());
///
/// path.push("/tmp/foo.rs");
/// assert!(!path.is_empty());
/// ```
#[unstable(feature = "path_is_empty", reason = "recently added", issue = "30259")]
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -3332,6 +3350,17 @@ mod tests {
}
}

#[test]
pub fn is_empty() {
let path = Path::new("/tmp/foo.rs");
assert!(!path.is_empty());

let mut path_buf = PathBuf::new();
assert!(path_buf.is_empty());
path_buf.push("foo");
assert!(!path_buf.is_empty());
}

#[test]
pub fn test_set_extension() {
macro_rules! tfe(
Expand Down