Skip to content

Commit ea43d7f

Browse files
committed
Implemented Path::to_str
1 parent df53a07 commit ea43d7f

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/path/path.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,26 @@ impl Path {
664664
pub fn to_path_buf(&self) -> PathBuf {
665665
PathBuf::from(self.inner.to_path_buf())
666666
}
667+
668+
/// Yields a [`&str`] slice if the `Path` is valid unicode.
669+
///
670+
/// This conversion may entail doing a check for UTF-8 validity.
671+
/// Note that validation is performed because non-UTF-8 strings are
672+
/// perfectly valid for some OS.
673+
///
674+
/// [`&str`]: https://doc.rust-lang.org/std/primitive.str.html
675+
///
676+
/// # Examples
677+
///
678+
/// ```
679+
/// use async_std::path::Path;
680+
///
681+
/// let path = Path::new("foo.txt");
682+
/// assert_eq!(path.to_str(), Some("foo.txt"));
683+
/// ```
684+
pub fn to_str(&self) -> Option<&str> {
685+
self.inner.to_str()
686+
}
667687
}
668688

669689
impl<'a> From<&'a std::path::Path> for &'a Path {

0 commit comments

Comments
 (0)