跳到主要内容

DirEntry

搜索

结构体 DirEntry 

Source
pub struct DirEntry { /* private fields */ }
展开描述

ReadDir 流所返回的条目。

This is a specialized version of std::fs::DirEntry for usage from the Tokio 运行时。

DirEntry 实例 表示文件系统上 目录内的一个条目。 每个条目 可以通过方法 进行检视, 以了解 完整路径, 或通过 特定于平台的 扩展 trait 了解其他元数据。

实现§

Source§

impl DirEntry

Source

pub fn path(&self) -> PathBuf

返回此条目所代表的文件的完整路径。

完整路径是通过将传递给 read_dir 的原始路径与此条目的 文件名连接起来创建的。

§示例
use tokio::fs;

let mut entries = fs::read_dir(".").await?;

while let Some(entry) = entries.next_entry().await? {
    println!("{:?}", entry.path());
}

这将输出类似如下内容:

"./whatever.txt"
"./foo.html"
"./hello_world.rs"

当然,确切的文本取决于 . 中有哪些文件。

Source

pub fn file_name(&self) -> OsString

返回此目录条目的裸文件名,不含任何其他 前导路径组件。

§示例
use tokio::fs;

let mut entries = fs::read_dir(".").await?;

while let Some(entry) = entries.next_entry().await? {
    println!("{:?}", entry.file_name());
}
Source

pub async fn metadata(&self) -> Result<Metadata>

返回此条目所指向的文件的元数据。

如果此条目指向符号链接,此函数不会遍历符号链接。

§Platform-specific behavior

在 Windows 上调用此函数开销很小(无需额外的系统调用), 但在 Unix 平台上,此函数等价于 对该路径调用 symlink_metadata

§示例
use tokio::fs;

let mut entries = fs::read_dir(".").await?;

while let Some(entry) = entries.next_entry().await? {
    if let Ok(metadata) = entry.metadata().await {
        // Now let's show our entry's permissions!
        println!("{:?}: {:?}", entry.path(), metadata.permissions());
    } else {
        println!("Couldn't get file type for {:?}", entry.path());
    }
}
Source

pub async fn file_type(&self) -> Result<FileType>

返回此条目所指向的文件的类型。

如果此条目指向符号链接,此函数不会遍历符号链接。

§Platform-specific behavior

在 Windows 和大多数 Unix 平台上,此函数无需任何代价(不需要额外 的系统调用),但某些 Unix 平台可能需要 通过等价的 symlink_metadata 调用来了解目标文件的类型。

§示例
use tokio::fs;

let mut entries = fs::read_dir(".").await?;

while let Some(entry) = entries.next_entry().await? {
    if let Ok(file_type) = entry.file_type().await {
        // Now let's show our entry's file type!
        println!("{:?}: {:?}", entry.path(), file_type);
    } else {
        println!("Couldn't get file type for {:?}", entry.path());
    }
}

Trait 实现§

Source§

impl Debug for DirEntry

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

使用给定的格式化器格式化此值。 更多信息

自动 Trait 实现§

Blanket 实现§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. 更多信息
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. 更多信息
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. 更多信息
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

原样返回传入的参数。

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

调用 U::from(self)

也就是说,此转换的具体行为取决于 From<T> for U 的实现方式。

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

转换出错时返回的类型。
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

执行转换。
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

转换出错时返回的类型。
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

执行转换。