Skip to content

Add documentation for PathBuf's FromIterator and Extend impls #142236

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

Merged
merged 1 commit into from
Jun 16, 2025
Merged
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
27 changes: 27 additions & 0 deletions library/std/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1882,6 +1882,19 @@ impl FromStr for PathBuf {

#[stable(feature = "rust1", since = "1.0.0")]
impl<P: AsRef<Path>> FromIterator<P> for PathBuf {
/// Creates a new `PathBuf` from the [`Path`] elements of an iterator.
///
/// This uses [`push`](Self::push) to add each element, so can be used to adjoin multiple path
/// [components](Components).
///
/// # Examples
/// ```
/// # use std::path::PathBuf;
/// let path = PathBuf::from_iter(["/tmp", "foo", "bar"]);
/// assert_eq!(path, PathBuf::from("/tmp/foo/bar"));
/// ```
///
/// See documentation for [`push`](Self::push) for more details on how the path is constructed.
fn from_iter<I: IntoIterator<Item = P>>(iter: I) -> PathBuf {
let mut buf = PathBuf::new();
buf.extend(iter);
Expand All @@ -1891,6 +1904,20 @@ impl<P: AsRef<Path>> FromIterator<P> for PathBuf {

#[stable(feature = "rust1", since = "1.0.0")]
impl<P: AsRef<Path>> Extend<P> for PathBuf {
/// Extends `self` with [`Path`] elements from `iter`.
///
/// This uses [`push`](Self::push) to add each element, so can be used to adjoin multiple path
/// [components](Components).
///
/// # Examples
/// ```
/// # use std::path::PathBuf;
/// let mut path = PathBuf::from("/tmp");
/// path.extend(["foo", "bar", "file.txt"]);
/// assert_eq!(path, PathBuf::from("/tmp/foo/bar/file.txt"));
/// ```
///
/// See documentation for [`push`](Self::push) for more details on how the path is constructed.
fn extend<I: IntoIterator<Item = P>>(&mut self, iter: I) {
iter.into_iter().for_each(move |p| self.push(p.as_ref()));
}
Expand Down
Loading