Skip to content

Commit 9755fe8

Browse files
author
Ali Raheem
committed
Add fs::read_dir() and ReadDir warning about iterator order + example
1 parent d4abb08 commit 9755fe8

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/libstd/fs.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ pub struct Metadata(fs_imp::FileAttr);
114114
/// information like the entry's path and possibly other metadata can be
115115
/// learned.
116116
///
117+
/// #### Note: Iteration Order is Implementation-Defined
118+
///
119+
/// The order in which this iterator returns entries is platform and filesystem
120+
/// dependent.
121+
///
117122
/// # Errors
118123
///
119124
/// 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<()> {
19591964
///
19601965
/// [changes]: ../io/index.html#platform-specific-behavior
19611966
///
1967+
/// #### Note: Iteration Order is Implementation-Defined
1968+
///
1969+
/// The order in which this iterator returns entries is platform and filesystem
1970+
/// dependent.
1971+
///
19621972
/// # Errors
19631973
///
19641974
/// 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<()> {
19912001
/// Ok(())
19922002
/// }
19932003
/// ```
2004+
///
2005+
/// ```rust,no_run
2006+
/// use std::{fs, io};
2007+
///
2008+
/// fn main() -> io::Result<()> {
2009+
/// // The order read_dir returns entries is not guaranteed. If reproducible
2010+
/// // ordering is required the entries should be explicitly sorted.
2011+
/// let mut entries = fs::read_dir(".")?
2012+
/// .map(|res| res.map(|e| e.path()))
2013+
/// .collect::<Result<Vec<_>, io::Error>>()?;
2014+
///
2015+
/// println!(
2016+
/// "Entries before sorting (may or may not be sorted already): {:?}",
2017+
/// entries
2018+
/// );
2019+
///
2020+
/// entries.sort();
2021+
///
2022+
/// println!("Entries after sorting: {:?}", entries);
2023+
/// Ok(())
2024+
/// }
2025+
/// ```
19942026
#[stable(feature = "rust1", since = "1.0.0")]
19952027
pub fn read_dir<P: AsRef<Path>>(path: P) -> io::Result<ReadDir> {
19962028
fs_imp::readdir(path.as_ref()).map(ReadDir)

0 commit comments

Comments
 (0)