Skip to content

Commit a0240fa

Browse files
committed
Add documentation for PathBuf's FromIterator and Extend impls
1 parent 68ac5ab commit a0240fa

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

library/std/src/path.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1882,6 +1882,19 @@ impl FromStr for PathBuf {
18821882

18831883
#[stable(feature = "rust1", since = "1.0.0")]
18841884
impl<P: AsRef<Path>> FromIterator<P> for PathBuf {
1885+
/// Creates a new `PathBuf` from the [`Path`] elements of an iterator.
1886+
///
1887+
/// This uses [`push`](Self::push) to add each element, so can be used to adjoin multiple path
1888+
/// [components](Components).
1889+
///
1890+
/// # Examples
1891+
/// ```
1892+
/// # use std::path::PathBuf;
1893+
/// let path = PathBuf::from_iter(["/tmp", "foo", "bar"]);
1894+
/// assert_eq!(path, PathBuf::from("/tmp/foo/bar"));
1895+
/// ```
1896+
///
1897+
/// See documentation for [`push`](Self::push) for more details on how the path is constructed.
18851898
fn from_iter<I: IntoIterator<Item = P>>(iter: I) -> PathBuf {
18861899
let mut buf = PathBuf::new();
18871900
buf.extend(iter);
@@ -1891,6 +1904,19 @@ impl<P: AsRef<Path>> FromIterator<P> for PathBuf {
18911904

18921905
#[stable(feature = "rust1", since = "1.0.0")]
18931906
impl<P: AsRef<Path>> Extend<P> for PathBuf {
1907+
/// Extends `self` with [`Path`] elements from `iter`.
1908+
///
1909+
/// This uses [`push`](Self::push) to add each element, so can be used to adjoin multiple path [components](Components).
1910+
///
1911+
/// # Examples
1912+
/// ```
1913+
/// # use std::path::PathBuf;
1914+
/// let mut path = PathBuf::from("/tmp");
1915+
/// path.extend(["foo", "bar", "file.txt"]);
1916+
/// assert_eq!(path, PathBuf::from("/tmp/foo/bar/file.txt"));
1917+
/// ```
1918+
///
1919+
/// See documentation for [`push`](Self::push) for more details on how the path is constructed.
18941920
fn extend<I: IntoIterator<Item = P>>(&mut self, iter: I) {
18951921
iter.into_iter().for_each(move |p| self.push(p.as_ref()));
18961922
}

0 commit comments

Comments
 (0)