If the intention is to read bytes from a file, consider using
!FileType::is_dir
instead of FileType::is_file
, or ensure that the file is
a symlink. FileType::is_file
itself is mutually exclusive to is_symlink()
and hence not all readable files will return is_file()
true.
Using !FileType::is_dir
is a simpler way of checking if a given file can be
read from or written to.
use std::{fs, io};
fn main() -> io::Result<()> {
let metadata = fs::metadata("foo.txt")?;
// this does not account for all readable files
if metadata.file_type().is_file() {
// ...
}
}
use std::{fs, io};
fn main() -> io::Result<()> {
let metadata = fs::metadata("foo.txt")?;
if !metadata.file_type().is_dir() {
// ...
}
}