< 返回版块

MicroIdea 发表于 2021-09-13 17:45

read_dir 迭代的时候给到的是一个 io::Result<DirEntry>,文档里面只是简单说了 New errors may be encountered after an iterator is initially constructed.

但是具体这个 New errors 到底是什么?

我试过了在迭代的时候对文件夹或者里面的文件作删除、重命名、改变权限,都没有返回 error; (测试平台包括 Mac 和 Linux,没有 windows 暂时没测)。

screenshot-20210913-174925.png

/// Iterator over the entries in a directory.
///
/// This iterator is returned from the [`read_dir`] function of this module and
/// will yield instances of [`io::Result`]`<`[`DirEntry`]`>`. Through a [`DirEntry`]
/// information like the entry's path and possibly other metadata can be
/// learned.
///
/// 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
/// IO error during iteration.
#[stable(feature = "rust1", since = "1.0.0")]
#[derive(Debug)]
pub struct ReadDir(fs_imp::ReadDir);

This [io::Result] will be an [Err] if there's some sort of intermittent IO error during iteration.

看起来一定要是比较罕见的 IO 错误?

评论区

写评论
Bai-Jinlin 2021-09-13 18:12

俗话说得好:RTFSC。

https://doc.rust-lang.org/stable/src/std/sys/unix/fs.rs.html#1018。 https://doc.rust-lang.org/stable/src/std/sys/unix/fs.rs.html#456。 可以看出来unix平台下read_dir会出问题的地方是opendir(3),ReadDir迭代的时候会出问题的地方是readdir(3),这两个用man 3就能查到到底什么情况会出错。对于read_dir你看https://linux.die.net/man/3/opendir这个就行。

作者 MicroIdea 2021-09-13 18:06

因为在其它语言里包括 C、Go、Python 等挺多语言里面好像都没有这个判断,唯独 rust 里面有,比较好奇为什么

1 共 2 条评论, 1 页