Skip to content

Commit 21a3d14

Browse files
authored
Unrolled build for #142236
Rollup merge of #142236 - yotamofek:pr/std/pathbuf-extend-docs, r=tgross35 Add documentation for `PathBuf`'s `FromIterator` and `Extend` impls I think it's not very obvious that `PathBuf`'s `Extend` and `FromIterator` impls work like `PathBuf::push`, so I think these should be documented. I'm not very happy with the wording and examples, open to suggestions :)
2 parents d9ca9bd + 45bbb3d commit 21a3d14

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

library/std/src/path.rs

Lines changed: 27 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,20 @@ 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
1910+
/// [components](Components).
1911+
///
1912+
/// # Examples
1913+
/// ```
1914+
/// # use std::path::PathBuf;
1915+
/// let mut path = PathBuf::from("/tmp");
1916+
/// path.extend(["foo", "bar", "file.txt"]);
1917+
/// assert_eq!(path, PathBuf::from("/tmp/foo/bar/file.txt"));
1918+
/// ```
1919+
///
1920+
/// See documentation for [`push`](Self::push) for more details on how the path is constructed.
18941921
fn extend<I: IntoIterator<Item = P>>(&mut self, iter: I) {
18951922
iter.into_iter().for_each(move |p| self.push(p.as_ref()));
18961923
}

0 commit comments

Comments
 (0)