Skip to content
CryoCryo home

dir

import std::fs::dir; · source

Types

ReadDir

type struct ReadDir {
    dirp: void*;
    base: String;
}

read_dir(p) opens a directory for iteration. ReadDir is a forward iterator yielding owned DirEntry values and skipping . and ..; order is filesystem-defined, and dropping it closes the handle.

import std::fs::dir;

mut entries: ReadDir = read_dir(p)?;
for (e in entries) {
    if (e.is_file()) { println(f"{e.name()}"); }
}

Properties

PropertyType
dirpvoid*
baseString

DirEntry

type struct DirEntry {
    entry_name: String;
    dtype:      u8;

    name(&this) -> Str;
    file_type(&this) -> FileType;
    is_dir(&this) -> boolean;
    is_file(&this) -> boolean;
}

DirEntry owns its name. On Linux the type comes from the dirent d_type field, which some filesystems report as unknown — that surfaces as FileType::Other, so call metadata on the full path when you need an authoritative answer. On Windows, where mingw's dirent has no d_type, each entry's Win32 attributes are read as it is yielded.

Properties
PropertyType
entry_nameString
dtypeu8
Methods
name(&this) -> Str;

The entry's file name (no directory components). Borrows this entry's storage.

file_type(&this) -> FileType;

File type from the dirent d_type field. Some filesystems report DT_UNKNOWN; call metadata on the full path for an authoritative answer in that case.

is_dir(&this) -> boolean;
is_file(&this) -> boolean;

Trait implementations

ReadDir
implement trait Iterator<DirEntry> for struct ReadDir
implement trait Drop for struct ReadDir
DirEntry
implement trait Drop for struct DirEntry

Free Functions

Directory operations

FunctionNotes
create_dir(p)One directory, mode 0755. Fails if it exists or a parent is missing.
create_dir_all(p)mkdir -p. Succeeds if p already exists as a directory, and tolerates a concurrent creator racing on the same path.
remove_file(p)unlink(2) — files and symlinks.
remove_dir(p)rmdir(2) — must be empty.
remove_dir_all(p)rm -r. Symlinked entries are unlinked, not followed. Returns the first failure.
rename(from, to)rename(2) — atomic within a filesystem, EXDEV across one.
canonicalize(p)The absolute path with every symlink and .. resolved.

function read_dir(p: Path) -> Result<ReadDir, IoError>;

Open p for iteration. The returned ReadDir must be dropped to close the directory handle.

function create_dir(p: Path) -> Result<(), IoError>;

Create a single directory (mode 0755). Fails if it already exists or a parent component is missing; use create_dir_all for the latter.

function create_dir_all(p: Path) -> Result<(), IoError>;

Recursively create p and any missing parent directories, like mkdir -p. Succeeds if p already exists as a directory. Each created component uses mode 0755. A concurrent creator racing on the same path is tolerated (an AlreadyExists that resolves to a directory is success); any other failure is returned as-is.

function remove_file(p: Path) -> Result<(), IoError>;

Remove a file (or symlink). unlink(2).

function remove_dir(p: Path) -> Result<(), IoError>;

Remove an empty directory. rmdir(2).

function remove_dir_all(p: Path) -> Result<(), IoError>;

Recursively remove p and everything under it, like rm -r on a directory. p must be a real directory. Entries that are symlinks are unlinked, not followed (a symlink reports a non-directory d_type, so it routes to remove_file). Returns the first failure. Relies on the directory's d_type; the supported Linux filesystems (ext4, btrfs, xfs, tmpfs) report entry types correctly.

function rename(from: Path, to: Path) -> Result<(), IoError>;

Rename / move from to to. rename(2) (atomic within a filesystem; fails with EXDEV across filesystems).

function canonicalize(p: Path) -> Result<PathBuf, IoError>;

Resolve p to an absolute, symlink-free canonical path (realpath(3)). The result fits in PATH_MAX (4096) bytes.

Constants

const DT_DIR:                       u8  = 4;
const DT_REG:                       u8  = 8;
const DT_LNK:                       u8  = 10;
const DIRENT_OFF_TYPE_LINUX:        u64 = 18;
const FILE_ATTRIBUTE_DIRECTORY:     u32 = 0x10;
const FILE_ATTRIBUTE_REPARSE_POINT: u32 = 0x400;
const INVALID_FILE_ATTRIBUTES:      u32 = 0xFFFFFFFF;
const DEFAULT_DIR_MODE:             u32 = 493;