Skip to main content

Dir

Struct Dir 

Source
pub struct Dir {
    inner: Dir,
}
🔬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(())
}

Fields§

§inner: Dir
🔬This is a nightly-only experimental API. (dirfd #120426)

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.

This function opens a directory. To open a file instead, see File::open.

§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_with<P: AsRef<Path>>(path: P, opts: &OpenOptions) -> Result<Self>

🔬This is a nightly-only experimental API. (dirfd #120426)

Attempts to open a directory at path according to opts.

This function opens a directory. To open a file instead, see File::open.

§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, OpenOptions}, io};

fn main() -> std::io::Result<()> {
    let dir = Dir::open_with("foo", &OpenOptions::new().read(true))?;
    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_for_traversal<P: AsRef<Path>>(path: P) -> Result<Self>

🔬This is a nightly-only experimental API. (dirfd #120426)

Attempts to open a directory at path with the minimum permissions for traversal.

The permissions requested by this function are guaranteed to be sufficient to open a child file or folder, but not necessarily to list all children.

§Errors

This function may return an error according to OpenOptions::open.

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

fn main() -> std::io::Result<()> {
    let foo = Dir::open_for_traversal("foo")?;
    let foobar = foo.open_dir("bar")?;
    let mut foobarbaz = foobar.open_file("baz")?;
    let contents = io::read_to_string(foobarbaz)?;
    assert_eq!(contents, "Hello, world!");
    Ok(())
}
Source

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

🔬This is a nightly-only experimental API. (dirfd #120426)

Queries metadata about the underlying directory.

§Examples
#![feature(dirfd)]
use std::fs::Dir;

fn main() -> std::io::Result<()> {
    let dir = Dir::open("foo")?;
    let metadata = dir.metadata()?;
    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.

This function interprets path relative to the directory provided by self. To open a file relative to the current working directory, or at an absolute path, see File::open.

§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(())
}
Source

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

🔬This is a nightly-only experimental API. (dirfd #120426)

Attempts to open a file according to opts relative to this directory.

This function interprets path relative to the directory provided by self. To open a file relative to the current working directory, or at an absolute path, see File::open.

§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, OpenOptions}, io::{self, Write}};

fn main() -> io::Result<()> {
    let dir = Dir::open("foo")?;
    let mut opts = OpenOptions::new();
    opts.read(true).write(true);
    let mut f = dir.open_file_with("bar.txt", &opts)?;
    f.write_all(b"Hello, world!")?;
    let contents = io::read_to_string(f)?;
    assert_eq!(contents, "Hello, world!");
    Ok(())
}
Source

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

🔬This is a nightly-only experimental API. (dirfd #120426)

Attempts to remove a file relative to this directory.

This function interprets path relative to the directory provided by self. To remove a file relative to the current working directory, or at an absolute path, see fs::remove_file.

§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;

fn main() -> std::io::Result<()> {
    let dir = Dir::open("foo")?;
    dir.remove_file("bar.txt")?;
    Ok(())
}
Source

pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>( &self, from: P, to_dir: &Self, to: Q, ) -> Result<()>

🔬This is a nightly-only experimental API. (dirfd #120426)

Attempts to rename a file or directory relative to this directory to a new name, replacing the destination file if present.

This function interprets from relative to the directory provided by self and to relative to the directory provided by to_dir. To rename a file relative to the current working directory, or at an absolute path, see fs::rename.

§Errors

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

§Examples
#![feature(dirfd)]
use std::fs::Dir;

fn main() -> std::io::Result<()> {
    let dir = Dir::open("foo")?;
    dir.rename("bar.txt", &dir, "quux.txt")?;
    Ok(())
}
Source

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

🔬This is a nightly-only experimental API. (dirfd #120426)

Attempts to create a directory relative to this directory.

This function interprets path relative to the directory provided by self. To create a directory relative to the current working directory, or at an absolute path, see fs::create_dir.

Source

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

🔬This is a nightly-only experimental API. (dirfd #120426)

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

This function interprets path relative to the directory provided by self. To open a directory relative to the current working directory, or at an absolute path, see Dir::open.

§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};

fn main() -> std::io::Result<()> {
    let dir = Dir::open("foo")?;
    let foobar = dir.open_dir("bar")?;
    Ok(())
}
Source

pub fn open_dir_with<P: AsRef<Path>>( &self, path: P, opts: &OpenOptions, ) -> Result<Self>

🔬This is a nightly-only experimental API. (dirfd #120426)

Attempts to open a directory relative to this directory according to opts.

This function interprets path relative to the directory provided by self. To open a directory relative to the current working directory, or at an absolute path, see Dir::open.

§Errors

This function will return errors according to OpenOptions::open.

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

fn main() -> std::io::Result<()> {
    let dir = Dir::open("foo")?;
    let foobar_w = dir.open_dir_with("bar", &OpenOptions::new().write(true))?;
    Ok(())
}
Source

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

🔬This is a nightly-only experimental API. (dirfd #120426)

Attempts to remove a directory relative to this directory.

This function interprets path relative to the directory provided by self. To remove a directory relative to the current working directory, or at an absolute path, see fs::remove_dir.

§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};

fn main() -> std::io::Result<()> {
    let dir = Dir::open("foo")?;
    dir.remove_dir("bar")?;
    Ok(())
}

Trait Implementations§

Source§

impl AsFd for Dir

Source§

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

Available on Hermit or Motor OS or Trusty or Unix or WASI only.
Borrows the file descriptor. Read more
Source§

impl AsInner<Dir> for Dir

Source§

fn as_inner(&self) -> &Dir

Source§

impl AsRawFd for Dir

Source§

fn as_raw_fd(&self) -> RawFd

Available on Hermit or Motor OS or Trusty or Unix or WASI 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 FromInner<Dir> for Dir

Source§

impl FromRawFd for Dir

Source§

unsafe fn from_raw_fd(fd: RawFd) -> Self

Available on Hermit or Motor OS or Trusty or Unix or WASI only.
Constructs a new instance of Self from the given raw file descriptor. Read more
Source§

impl IntoInner<Dir> for Dir

Source§

impl IntoRawFd for Dir

Source§

fn into_raw_fd(self) -> RawFd

Available on Hermit or Motor OS or Trusty or Unix or WASI only.
Consumes this object, returning the raw underlying file descriptor. Read more

Auto Trait Implementations§

§

impl Freeze for Dir

§

impl RefUnwindSafe for Dir

§

impl Send for Dir

§

impl Sync for Dir

§

impl Unpin for Dir

§

impl UnsafeUnpin for Dir

§

impl UnwindSafe for Dir

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> SizeHint for T
where T: ?Sized,

Source§

default fn lower_bound(&self) -> usize

🔬This is a nightly-only experimental API. (core_io_internals)
Returns a lower bound on the number of elements this container-like item contains. For example, an array [u8; 12] could return any value between 0 and 12 inclusively as a correct implementation. Read more
Source§

default fn upper_bound(&self) -> Option<usize>

🔬This is a nightly-only experimental API. (core_io_internals)
Returns an upper bound on the number of elements this container-like item contains if it can be determined, otherwise None. Read more
Source§

final fn size_hint(&self) -> (usize, Option<usize>)

🔬This is a nightly-only experimental API. (core_io_internals)
Returns an estimate for the number of elements this container like type contains. Read more
Source§

impl<T> SizedTypeProperties for T

Source§

#[doc(hidden)]
const SIZE: usize = _

🔬This is a nightly-only experimental API. (sized_type_properties)
Source§

#[doc(hidden)]
const ALIGN: usize = _

🔬This is a nightly-only experimental API. (sized_type_properties)
Source§

#[doc(hidden)]
const ALIGNMENT: Alignment = _

🔬This is a nightly-only experimental API. (ptr_alignment_type #102070)
Source§

#[doc(hidden)]
const IS_ZST: bool = _

🔬This is a nightly-only experimental API. (sized_type_properties)
true if this type requires no storage. false if its size is greater than zero. Read more
Source§

#[doc(hidden)]
const LAYOUT: Layout = _

🔬This is a nightly-only experimental API. (sized_type_properties)
Source§

#[doc(hidden)]
const MAX_SLICE_LEN: usize = _

🔬This is a nightly-only experimental API. (sized_type_properties)
The largest safe length for a [Self]. Read more
Source§

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

Source§

type Error = !

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.