diff options
| author | Ali Raheem <github@hadoken.cc> | 2019-08-07 15:59:18 +0100 |
|---|---|---|
| committer | Ali Raheem <github@hadoken.cc> | 2019-08-07 15:59:18 +0100 |
| commit | 9755fe80ee88803f2ecf7e86d7ae6d6fb8e25f55 (patch) | |
| tree | 508f2d03755a2eed39c3edba177cec8197af4125 /src/libstd | |
| parent | d4abb08be6c3a06a14e285396f5e3ef367584f77 (diff) | |
| download | rust-9755fe80ee88803f2ecf7e86d7ae6d6fb8e25f55.tar.gz rust-9755fe80ee88803f2ecf7e86d7ae6d6fb8e25f55.zip | |
Add fs::read_dir() and ReadDir warning about iterator order + example
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/fs.rs | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/libstd/fs.rs b/src/libstd/fs.rs index f7c32a5c20d..2a55c79a93c 100644 --- a/src/libstd/fs.rs +++ b/src/libstd/fs.rs @@ -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 +/// +/// 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 @@ -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 @@ -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 +/// // 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) |
