Skip to content
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

Issue#63183: Add fs::read_dir() and ReadDir warning about iterator order + example #63356

Merged
merged 5 commits into from
Sep 25, 2019
Merged
Changes from 1 commit
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
32 changes: 32 additions & 0 deletions src/libstd/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ pub struct Metadata(fs_imp::FileAttr);
/// information like the entry's path and possibly other metadata can be
/// learned.
///
/// #### Note: Iteration Order is Implementation-Defined
Centril marked this conversation as resolved.
Show resolved Hide resolved
///
/// The order in which this iterator returns entries is platform and filesystem
/// dependent.
///
/// # Errors
///
/// This [`io::Result`] will be an [`Err`] if there's some sort of intermittent
Expand Down Expand Up @@ -1959,6 +1964,11 @@ pub fn remove_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
///
/// [changes]: ../io/index.html#platform-specific-behavior
///
/// #### Note: Iteration Order is Implementation-Defined
///
/// The order in which this iterator returns entries is platform and filesystem
/// dependent.
///
/// # Errors
///
/// This function will return an error in the following situations, but is not
Expand Down Expand Up @@ -1991,6 +2001,28 @@ pub fn remove_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
/// Ok(())
/// }
/// ```
///
/// ```rust,no_run
/// use std::{fs, io};
///
/// fn main() -> io::Result<()> {
/// // The order read_dir returns entries is not guaranteed. If reproducible
ali-raheem marked this conversation as resolved.
Show resolved Hide resolved
/// // ordering is required the entries should be explicitly sorted.
/// let mut entries = fs::read_dir(".")?
/// .map(|res| res.map(|e| e.path()))
/// .collect::<Result<Vec<_>, io::Error>>()?;
///
/// println!(
/// "Entries before sorting (may or may not be sorted already): {:?}",
/// entries
/// );
///
/// entries.sort();
///
/// println!("Entries after sorting: {:?}", entries);
/// Ok(())
/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn read_dir<P: AsRef<Path>>(path: P) -> io::Result<ReadDir> {
fs_imp::readdir(path.as_ref()).map(ReadDir)
Expand Down