Skip to main content

Dir

Struct Dir 

Source
pub struct Dir { /* private fields */ }
๐Ÿ”ฌThis is a nightly-only experimental API. (dirfd #120426)
Expand description

An object providing access to a directory on the filesystem.

Directories are automatically closed when they go out of scope. Errors detected on closing are ignored by the implementation of Drop.

ยงPlatform-specific behavior

On supported systems (including Windows and some UNIX-based OSes), this function acquires a handle/file descriptor for the directory. This allows functions like Dir::open_file to avoid TOCTOU errors when the directory itself is being moved.

On other systems, it stores an absolute path (see canonicalize()). In the latter case, no TOCTOU guarantees are made.

ยงExamples

Opens a directory and then a file inside it.

#![feature(dirfd)]
use std::{fs::Dir, io};

fn main() -> std::io::Result<()> {
    let dir = Dir::open("foo")?;
    let mut file = dir.open_file("bar.txt")?;
    let contents = io::read_to_string(file)?;
    assert_eq!(contents, "Hello, world!");
    Ok(())
}

Implementationsยง

Sourceยง

impl Dir

Source

pub fn open<P: AsRef<Path>>(path: P) -> Result<Self>

๐Ÿ”ฌThis is a nightly-only experimental API. (dirfd #120426)

Attempts to open a directory at path in read-only mode.

ยงErrors

This function will return an error if path does not point to an existing directory. Other errors may also be returned according to OpenOptions::open.

ยงExamples
#![feature(dirfd)]
use std::{fs::Dir, io};

fn main() -> std::io::Result<()> {
    let dir = Dir::open("foo")?;
    let mut f = dir.open_file("bar.txt")?;
    let contents = io::read_to_string(f)?;
    assert_eq!(contents, "Hello, world!");
    Ok(())
}
Source

pub fn open_file<P: AsRef<Path>>(&self, path: P) -> Result<File>

๐Ÿ”ฌThis is a nightly-only experimental API. (dirfd #120426)

Attempts to open a file in read-only mode relative to this directory.

ยงErrors

This function will return an error if path does not point to an existing file. Other errors may also be returned according to OpenOptions::open.

ยงExamples
#![feature(dirfd)]
use std::{fs::Dir, io};

fn main() -> std::io::Result<()> {
    let dir = Dir::open("foo")?;
    let mut f = dir.open_file("bar.txt")?;
    let contents = io::read_to_string(f)?;
    assert_eq!(contents, "Hello, world!");
    Ok(())
}

Trait Implementationsยง

Sourceยง

impl AsFd for Dir

Sourceยง

fn as_fd(&self) -> BorrowedFd<'_>

Available on Unix or HermitCore or target_os=trusty or WASI or target_os=motor only.
Borrows the file descriptor. Read more
Sourceยง

impl AsRawFd for Dir

Sourceยง

fn as_raw_fd(&self) -> RawFd

Available on Unix or HermitCore or target_os=trusty or WASI or target_os=motor only.
Extracts the raw file descriptor. Read more
Sourceยง

impl Debug for Dir

Sourceยง

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

Formats the value using the given formatter. Read more
Sourceยง

impl From<Dir> for OwnedFd

Sourceยง

fn from(value: Dir) -> Self

Converts to this type from the input type.
Sourceยง

impl From<OwnedFd> for Dir

Sourceยง

fn from(value: OwnedFd) -> Self

Converts to this type from the input type.
Sourceยง

impl FromRawFd for Dir

Sourceยง

unsafe fn from_raw_fd(fd: RawFd) -> Self

Available on Unix or HermitCore or target_os=trusty or WASI or target_os=motor only.
Constructs a new instance of Self from the given raw file descriptor. Read more
Sourceยง

impl IntoRawFd for Dir

Sourceยง

fn into_raw_fd(self) -> RawFd

Available on Unix or HermitCore or target_os=trusty or WASI or target_os=motor only.
Consumes this object, returning the raw underlying file descriptor. Read more

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, U> TryFrom<U> for T
where U: Into<T>,

Sourceยง

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>,

Sourceยง

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.