Struct std::fs::FileType

1.1.0 · source ·
pub struct FileType(/* private fields */);
Expand description

A structure representing a type of file with accessors for each file type. It is returned by Metadata::file_type method.

Implementations§

source§

impl FileType

source

pub fn is_dir(&self) -> bool

Tests whether this file type represents a directory. The result is mutually exclusive to the results of is_file and is_symlink; only zero or one of these tests may pass.

§Examples
fn main() -> std::io::Result<()> {
    use std::fs;

    let metadata = fs::metadata("foo.txt")?;
    let file_type = metadata.file_type();

    assert_eq!(file_type.is_dir(), false);
    Ok(())
}
Run
source

pub fn is_file(&self) -> bool

Tests whether this file type represents a regular file. The result is mutually exclusive to the results of is_dir and is_symlink; only zero or one of these tests may pass.

When the goal is simply to read from (or write to) the source, the most reliable way to test the source can be read (or written to) is to open it. Only using is_file can break workflows like diff <( prog_a ) on a Unix-like system for example. See File::open or OpenOptions::open for more information.

§Examples
fn main() -> std::io::Result<()> {
    use std::fs;

    let metadata = fs::metadata("foo.txt")?;
    let file_type = metadata.file_type();

    assert_eq!(file_type.is_file(), true);
    Ok(())
}
Run

Tests whether this file type represents a symbolic link. The result is mutually exclusive to the results of is_dir and is_file; only zero or one of these tests may pass.

The underlying Metadata struct needs to be retrieved with the fs::symlink_metadata function and not the fs::metadata function. The fs::metadata function follows symbolic links, so is_symlink would always return false for the target file.

§Examples
use std::fs;

fn main() -> std::io::Result<()> {
    let metadata = fs::symlink_metadata("foo.txt")?;
    let file_type = metadata.file_type();

    assert_eq!(file_type.is_symlink(), false);
    Ok(())
}
Run

Trait Implementations§

source§

impl Clone for FileType

source§

fn clone(&self) -> FileType

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for FileType

source§

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

Formats the value using the given formatter. Read more
1.5.0 · source§

impl FileTypeExt for FileType

Available on Unix only.
source§

fn is_block_device(&self) -> bool

Returns true if this file type is a block device. Read more
source§

fn is_char_device(&self) -> bool

Returns true if this file type is a char device. Read more
source§

fn is_fifo(&self) -> bool

Returns true if this file type is a fifo. Read more
source§

fn is_socket(&self) -> bool

Returns true if this file type is a socket. Read more
source§

impl FileTypeExt for FileType

Available on WASI only.
source§

fn is_block_device(&self) -> bool

🔬This is a nightly-only experimental API. (wasi_ext #71213)
Returns true if this file type is a block device.
source§

fn is_char_device(&self) -> bool

🔬This is a nightly-only experimental API. (wasi_ext #71213)
Returns true if this file type is a character device.
source§

fn is_socket_dgram(&self) -> bool

🔬This is a nightly-only experimental API. (wasi_ext #71213)
Returns true if this file type is a socket datagram.
source§

fn is_socket_stream(&self) -> bool

🔬This is a nightly-only experimental API. (wasi_ext #71213)
Returns true if this file type is a socket stream.
source§

fn is_socket(&self) -> bool

🔬This is a nightly-only experimental API. (wasi_ext #71213)
Returns true if this file type is any type of socket.
1.64.0 · source§

impl FileTypeExt for FileType

Available on Windows only.
Returns true if this file type is a symbolic link that is also a directory.
Returns true if this file type is a symbolic link that is also a file.
source§

impl Hash for FileType

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl PartialEq for FileType

source§

fn eq(&self, other: &FileType) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Copy for FileType

source§

impl Eq for FileType

source§

impl StructuralPartialEq for FileType

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
source§

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

Performs the conversion.