about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-09-24 23:45:13 +0200
committerGitHub <noreply@github.com>2019-09-24 23:45:13 +0200
commitc623aa4a54c875287858d7f524a9e4483ca2de64 (patch)
treed2cf8af8e69b6eb90bd264d65a5383411004f211 /src/libstd
parent6ef275e6c3cb1384ec78128eceeb4963ff788dca (diff)
parent1161aeb2b423da744e687315648a49cc4774220b (diff)
downloadrust-c623aa4a54c875287858d7f524a9e4483ca2de64.tar.gz
rust-c623aa4a54c875287858d7f524a9e4483ca2de64.zip
Rollup merge of #63356 - ali-raheem:issue#63183, r=KodrAus
Issue#63183: Add fs::read_dir() and ReadDir warning about iterator order + example

As per https://github.com/rust-lang/rust/issues/63183

Add warning about iterator order to read_dir and ReadDir, add example of explicitly ordering direntrys.
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/fs.rs25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/libstd/fs.rs b/src/libstd/fs.rs
index b5265fe369e..8933f027a06 100644
--- a/src/libstd/fs.rs
+++ b/src/libstd/fs.rs
@@ -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
@@ -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
@@ -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)