Skip to content

Commit a6e1abe

Browse files
committed
Implemented Path::file_name
1 parent a7eaae9 commit a6e1abe

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/path/path.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ impl Path {
183183
/// * Otherwise, the portion of the file name after the final `.`
184184
///
185185
/// [`self.file_name`]: struct.Path.html#method.file_name
186-
/// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html
186+
/// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None
187187
///
188188
/// # Examples
189189
///
@@ -198,6 +198,32 @@ impl Path {
198198
self.inner.extension()
199199
}
200200

201+
/// Returns the final component of the `Path`, if there is one.
202+
///
203+
/// If the path is a normal file, this is the file name. If it's the path of a directory, this
204+
/// is the directory name.
205+
///
206+
/// Returns [`None`] if the path terminates in `..`.
207+
///
208+
/// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None
209+
///
210+
/// # Examples
211+
///
212+
/// ```
213+
/// use async_std::path::Path;
214+
/// use std::ffi::OsStr;
215+
///
216+
/// assert_eq!(Some(OsStr::new("bin")), Path::new("/usr/bin/").file_name());
217+
/// assert_eq!(Some(OsStr::new("foo.txt")), Path::new("tmp/foo.txt").file_name());
218+
/// assert_eq!(Some(OsStr::new("foo.txt")), Path::new("foo.txt/.").file_name());
219+
/// assert_eq!(Some(OsStr::new("foo.txt")), Path::new("foo.txt/.//").file_name());
220+
/// assert_eq!(None, Path::new("foo.txt/..").file_name());
221+
/// assert_eq!(None, Path::new("/").file_name());
222+
/// ```
223+
pub fn file_name(&self) -> Option<&OsStr> {
224+
self.inner.file_name()
225+
}
226+
201227
/// Converts a [`Box<Path>`][`Box`] into a [`PathBuf`] without copying or
202228
/// allocating.
203229
///

0 commit comments

Comments
 (0)