Struct std::fs::DirEntry
ReadDir 迭代器返回的条目。ReadDir是一个迭代器,遍历这个迭代器,其中每个迭代项就是DirEntry
DirEntry 的实例表示文件系统上目录内的一个条目。 可以通过方法检查每个条目,以通过全平台扩展 traits 了解完整路径或可能的其他元数据。
TIP
特定于平台的行为
在 Unix 上,DirEntry 结构体包含对打开目录的内部引用。 即使在 ReadDir 迭代器丢弃之后,持有 DirEntry 对象也会消耗文件句柄。
注意这个 将来可能会发生变化。
Implementations
impl DirEntry
有这么一个文件夹,以下示例都读取这个文件夹

path
返回此条目表示的文件的完整路径。
通过将 read_dir 的原始路径与该条目的文件名连接起来,可以创建完整路径。
rust
pub fn path(&self) -> PathBuf返回值:返回当前遍历条目的路径
rust
use std::fs::{self};
fn main() {
let dir = fs::read_dir("./test_dir").unwrap();
for entry in dir {
let path = entry.unwrap().path();
println!("{:#?}", path);
}
/* 打印结果:
"./test_dir\\aaa.js"
"./test_dir\\bbb.html"
"./test_dir\\ccc.json"
*/
}metadata
返回此条目指向的文件的元数据。
如果此函数指向符号链接,则该函数将不会遍历符号链接。要遍历符号链接,请使用 fs::metadata) 或 fs::File::metadata。
rust
pub fn metadata(&self) -> Result<Metadata>返回值:返回一个Result,若读取成功则Ok()中包裹着Metadata,否则返回相应错误信息
rust
use std::fs::{self};
fn main() {
let dir = fs::read_dir("./test_dir").unwrap();
for entry in dir {
let path = entry.unwrap().metadata();
println!("{:#?}", path);
}
/* 打印结果:
Ok(
Metadata {
file_type: FileType {
is_file: true,
is_dir: false,
is_symlink: false,
..
},
permissions: Permissions(
FilePermissions {
attrs: 32,
},
),
len: 0,
modified: SystemTime {
intervals: 134222779659976890,
},
accessed: SystemTime {
intervals: 134222779659976890,
},
created: SystemTime {
intervals: 134222779659976890,
},
..
},
)
Ok(
Metadata {
file_type: FileType {
is_file: true,
is_dir: false,
is_symlink: false,
..
},
permissions: Permissions(
FilePermissions {
attrs: 32,
},
),
len: 0,
modified: SystemTime {
intervals: 134222779712944186,
},
accessed: SystemTime {
intervals: 134222779712944186,
},
created: SystemTime {
intervals: 134222779712944186,
},
..
},
)
Ok(
Metadata {
file_type: FileType {
is_file: true,
is_dir: false,
is_symlink: false,
..
},
permissions: Permissions(
FilePermissions {
attrs: 32,
},
),
len: 0,
modified: SystemTime {
intervals: 134222779886283353,
},
accessed: SystemTime {
intervals: 134222779886283353,
},
created: SystemTime {
intervals: 134222779886283353,
},
..
},
)
*/
}file_type
返回此条目指向的文件的文件类型。(不会遍历软链接)
rust
pub fn file_type(&self) -> Result<FileType>返回值:返回一个Result,若读取成功则Ok()中包裹着FileType,否则返回相应错误信息
rust
use std::fs::{self};
fn main() {
let dir = fs::read_dir("./test_dir").unwrap();
for entry in dir {
let path = entry.unwrap().file_type();
println!("{:#?}", path);
}
/* 打印结果:
Ok(
FileType {
is_file: true,
is_dir: false,
is_symlink: false,
..
},
)
Ok(
FileType {
is_file: true,
is_dir: false,
is_symlink: false,
..
},
)
Ok(
FileType {
is_file: true,
is_dir: false,
is_symlink: false,
..
},
)
*/
}file_name
返回此目录条目的裸文件名,不包含任何其他前导路径组件
rust
pub fn file_name(&self) -> OsString返回值:返回文件名字符串
rust
use std::fs::{self};
fn main() {
let dir = fs::read_dir("./test_dir").unwrap();
for entry in dir {
let path = entry.unwrap().file_name();
println!("{:#?}", path);
}
/* 打印结果:
"aaa.js"
"bbb.html"
"ccc.json"
*/
}Trait Implementations
impl Debug for DirEntry
fmt
使用给定的格式化程序格式化该值。
rust
fn fmt(&self, f: &mut Formatter<'_>) -> Resultimpl DirEntryExt for DirEntry
TIP
仅在Unix上可用
ino
返回所包含的 dirent 结构中的底层 d_ino 字段。
rust
fn ino(&self) -> u64impl DirEntryExt for DirEntry
TIP
仅在WASI上可用
ino
返回 dirent_t 的底层 d_ino 字段
rust
fn ino(&self) -> u64impl DirEntryExt2 for DirEntry
TIP
仅在Unix上可用
file_name_ref
返回指向此条目文件名的底层 OsStr 的引用。
rust
fn file_name_ref(&self) -> &OsStrAuto Trait Implementations
impl RefUnwindSafe for DirEntry
impl Send for DirEntry
impl Sync for DirEntry
impl Unpin for DirEntry
impl UnwindSafe for DirEntry
Blanket Implementations
impl<T> Any for T
rust
impl<T> Any for T
where
T: 'static + ?Sized,impl<T> Borrow<T> for T
rust
impl<T> Borrow<T> for T
where
T: ?Sized,impl<T> BorrowMut<T> for T
rust
impl<T> BorrowMut<T> for T
where
T: ?Sized,impl<T> From<T> for T
rust
impl<T> From<T> for Timpl<T, U> Into<U> for T
rust
impl<T, U> Into<U> for T
where
U: From<T>,impl<T, U> TryFrom<U> for T
rust
impl<T, U> TryFrom<U> for T
where
U: Into<T>,impl<T, U> TryInto<U> for T
rust
impl<T, U> TryInto<U> for T
where
U: TryFrom<T>,