Skip to content

Commit

Permalink
Rollup merge of rust-lang#63356 - ali-raheem:issue#63183, r=KodrAus
Browse files Browse the repository at this point in the history
Issue#63183: Add fs::read_dir() and ReadDir warning about iterator order + example

As per rust-lang#63183

Add warning about iterator order to read_dir and ReadDir, add example of explicitly ordering direntrys.
  • Loading branch information
Centril authored Sep 23, 2019
2 parents 66bf391 + 1161aeb commit 81ba34e
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/libstd/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ pub struct Metadata(fs_imp::FileAttr);
/// information like the entry's path and possibly other metadata can be
/// learned.
///
/// 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 @@ -1962,6 +1965,9 @@ pub fn remove_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
///
/// [changes]: ../io/index.html#platform-specific-behavior
///
/// 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 @@ -1994,6 +2000,25 @@ pub fn remove_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
/// Ok(())
/// }
/// ```
///
/// ```rust,no_run
/// use std::{fs, io};
///
/// fn main() -> io::Result<()> {
/// let mut entries = fs::read_dir(".")?
/// .map(|res| res.map(|e| e.path()))
/// .collect::<Result<Vec<_>, io::Error>>()?;
///
/// // The order in which `read_dir` returns entries is not guaranteed. If reproducible
/// // ordering is required the entries should be explicitly sorted.
///
/// entries.sort();
///
/// // The entries have now been sorted by their path.
///
/// 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

0 comments on commit 81ba34e

Please sign in to comment.