Skip to content

Commit 1669963

Browse files
Add DirEntry doc examples
1 parent dad29a6 commit 1669963

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

src/libstd/fs.rs

+55
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,26 @@ impl DirEntry {
846846
/// On Windows this function is cheap to call (no extra system calls
847847
/// needed), but on Unix platforms this function is the equivalent of
848848
/// calling `symlink_metadata` on the path.
849+
///
850+
/// # Examples
851+
///
852+
/// ```
853+
/// use std::fs;
854+
///
855+
/// if let Ok(entries) = fs::read_dir(".") {
856+
/// for entry in entries {
857+
/// if let Ok(entry) = entry {
858+
/// // Here, `entry` is a `DirEntry`.
859+
/// if let Ok(metadata) = entry.metadata() {
860+
/// // Now let's show our entry's permissions!
861+
/// println!("{:?}: {:?}", entry.path(), metadata.permissions());
862+
/// } else {
863+
/// println!("Couldn't get metadata for {:?}", entry.path());
864+
/// }
865+
/// }
866+
/// }
867+
/// }
868+
/// ```
849869
#[stable(feature = "dir_entry_ext", since = "1.1.0")]
850870
pub fn metadata(&self) -> io::Result<Metadata> {
851871
self.0.metadata().map(Metadata)
@@ -861,13 +881,48 @@ impl DirEntry {
861881
/// On Windows and most Unix platforms this function is free (no extra
862882
/// system calls needed), but some Unix platforms may require the equivalent
863883
/// call to `symlink_metadata` to learn about the target file type.
884+
///
885+
/// # Examples
886+
///
887+
/// ```
888+
/// use std::fs;
889+
///
890+
/// if let Ok(entries) = fs::read_dir(".") {
891+
/// for entry in entries {
892+
/// if let Ok(entry) = entry {
893+
/// // Here, `entry` is a `DirEntry`.
894+
/// if let Ok(file_type) = entry.file_type() {
895+
/// // Now let's show our entry's file type!
896+
/// println!("{:?}: {:?}", entry.path(), file_type);
897+
/// } else {
898+
/// println!("Couldn't get file type for {:?}", entry.path());
899+
/// }
900+
/// }
901+
/// }
902+
/// }
903+
/// ```
864904
#[stable(feature = "dir_entry_ext", since = "1.1.0")]
865905
pub fn file_type(&self) -> io::Result<FileType> {
866906
self.0.file_type().map(FileType)
867907
}
868908

869909
/// Returns the bare file name of this directory entry without any other
870910
/// leading path component.
911+
///
912+
/// # Examples
913+
///
914+
/// ```
915+
/// use std::fs;
916+
///
917+
/// if let Ok(entries) = fs::read_dir(".") {
918+
/// for entry in entries {
919+
/// if let Ok(entry) = entry {
920+
/// // Here, `entry` is a `DirEntry`.
921+
/// println!("{:?}", entry.file_name());
922+
/// }
923+
/// }
924+
/// }
925+
/// ```
871926
#[stable(feature = "dir_entry_ext", since = "1.1.0")]
872927
pub fn file_name(&self) -> OsString {
873928
self.0.file_name()

src/libstd/sys/unix/ext/fs.rs

+16
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,22 @@ impl FileTypeExt for fs::FileType {
196196
pub trait DirEntryExt {
197197
/// Returns the underlying `d_ino` field in the contained `dirent`
198198
/// structure.
199+
///
200+
/// # Examples
201+
///
202+
/// ```
203+
/// use std::fs;
204+
/// use std::os::unix::fs::DirEntryExt;
205+
///
206+
/// if let Ok(entries) = fs::read_dir(".") {
207+
/// for entry in entries {
208+
/// if let Ok(entry) = entry {
209+
/// // Here, `entry` is a `DirEntry`.
210+
/// println!("{:?}: {}", entry.file_name(), entry.ino());
211+
/// }
212+
/// }
213+
/// }
214+
/// ```
199215
#[stable(feature = "dir_entry_ext", since = "1.1.0")]
200216
fn ino(&self) -> u64;
201217
}

0 commit comments

Comments
 (0)