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
| Property | Type |
|---|---|
dirp | void* |
base | String |
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
| Property | Type |
|---|---|
entry_name | String |
dtype | u8 |
Methods
name(&this) -> Str;
name(&this) -> Str;The entry's file name (no directory components). Borrows this entry's storage.
file_type(&this) -> FileType;
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
| Function | Notes |
|---|---|
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>;
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>;
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>;
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>;
function remove_file(p: Path) -> Result<(), IoError>;Remove a file (or symlink). unlink(2).
function remove_dir(p: Path) -> Result<(), IoError>;
function remove_dir(p: Path) -> Result<(), IoError>;Remove an empty directory. rmdir(2).
function remove_dir_all(p: Path) -> Result<(), IoError>;
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>;
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>;
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;type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct ReadDir {
dirp: void*;
base: String;
}Forward iterator over the entries of a directory. Yields owned DirEntry values, skipping . and ... Closes the underlying DIR* on drop. Order is filesystem-defined.
ReadDir.dirp: void*ReadDir.base: Stringtype struct String<A = GlobalAlloc> {
buffer: RawBuffer<u8, A>;
length: u64;
}String: owned, heap-allocated, length-typed UTF-8.
std::collections::stringimport a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
mut name: Type = value;A mutable binding, reassignable after declaration - mutability is opt-in, const is the default. At module level it declares mutable global state. Before & it marks an exclusive reference or receiver instead.
mut entries: ReadDirtype struct ReadDir {
dirp: void*;
base: String;
}Forward iterator over the entries of a directory. Yields owned DirEntry values, skipping . and ... Closes the underlying DIR* on drop. Order is filesystem-defined.
function read_dir(p: Path) -> Result<ReadDir, IoError>Open p for iteration. The returned ReadDir must be dropped to close the directory handle.
value?Error propagation. On a Result<T, E> it yields the T of an Ok and otherwise returns the Err(e) from the enclosing function unchanged; on an Option<T> it yields the T of a Some or returns None. The enclosing function must return a matching Result / Option.
for (mut i: i32 = 0; i < n; i++) { ... }
for (item in iterable) { ... }Two forms. The C-style for (init; condition; step) scopes its loop variable to the body. for (x in expr) iterates anything with next() -> Option<T>: an iterator, a range such as 0..n, an Array or Slice (their iter() is inserted for you), or a fixed-size array.
e: DirEntryfor (item in iterable) { ... }Separates the loop variable from the sequence in a for loop. The sequence is evaluated once; the loop lowers to loop { match (it.next()) { Some(x) => .., None => break } }.
mut entries: ReadDirif (condition) { ... } else if (condition) { ... } else { ... }Branches on a parenthesised boolean condition. Bodies are always braced; there is no single-statement form. Used as an expression, both arms are required and must produce the same type: const sign = if (n < 0) { -1 } else { 1 };.
e: DirEntryDirEntry::is_file(&this) -> booleanDirEntry::name(&this) -> StrThe entry's file name (no directory components). Borrows this entry's storage.
std::fs::dirtype struct DirEntry {
entry_name: String;
dtype: u8;
}One entry returned by ReadDir. Owns its name; drop releases it.
DirEntry.entry_name: StringDirEntry.dtype: u8DirEntry::name(&this) -> StrThe entry's file name (no directory components). Borrows this entry's storage.
&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
type struct Str {
bytes: u8*;
length: u64;
}Str: a borrowed UTF-8 string slice.
std::collections::strDirEntry::file_type(&this) -> FileTypeFile 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.
type enum FileType {
File;
Dir;
Symlink;
Other;
}The broad category of a filesystem entry.
std::fs::metadataDirEntry::is_dir(&this) -> booleanDirEntry::is_file(&this) -> booleanconst NAME: Type = value;At module level, a true compile-time constant, conventionally SCREAMING_SNAKE_CASE. (mut at module level declares mutable global state instead.)
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;type struct DirEntry {
entry_name: String;
dtype: u8;
}One entry returned by ReadDir. Owns its name; drop releases it.
ReadDir.dirp: void*ReadDir.base: StringFileType::OtherDirEntry.entry_name: StringDirEntry.dtype: u8DirEntry::file_type(&this) -> FileTypeFile 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.
DirentType::DT_UNKNOWN = 0DirEntry::is_dir(&this) -> booleanimplement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
implement trait Trait for Type { ... }Introduces a trait implementation: the block supplies, for one concrete type, the method bodies the trait requires.
language referencetype trait Iterator {
type Item;
next(mut &this) -> Option<This::Item>;
count(mut &this) -> u64;
fold<Acc>(mut &this, initial: Acc, f: (Acc, This::Item) -> Acc) -> Acc;
for_each(mut &this, f: (This::Item) -> void) -> void;
take(this, n: u64) -> TakeIter<This>;
skip(this, n: u64) -> SkipIter<This>;
map<B>(this, f: (This::Item) -> B) -> MapIter<This, B>;
filter(this, pred: (&This::Item) -> boolean) -> FilterIter<This>;
chain<J>(this, other: J) -> ChainIter<This, J>;
// … 7 more
}implement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language referencefunction 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.
extern "C" function mkdir(path: u8*, mode: u32) -> i32function remove_file(p: Path) -> Result<(), IoError>Remove a file (or symlink). unlink(2).
extern "C" function unlink(path: u8*) -> i32extern "C" function rmdir(path: u8*) -> i32function 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.
function rename(from: Path, to: Path) -> Result<(), IoError>Rename / move from to to. rename(2) (atomic within a filesystem; fails with EXDEV across filesystems).
// reservedA reserved keyword with no syntax of its own in the current grammar. The standard library uses it freely as a method and parameter name (Str::from, from: u64).
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.
function name<T>(param: Type) -> Ret { ... }Declares a function. Parameters are written name: Type and are never inferred; the return type follows -> and defaults to void. Declaration order does not matter: every signature is collected before any body is checked, so functions may call each other freely and recurse.
function read_dir(p: Path) -> Result<ReadDir, IoError>Open p for iteration. The returned ReadDir must be dropped to close the directory handle.
(parameter) p: Pathtype struct Path {
inner: Str;
}Path / PathBuf: filesystem paths as length-typed UTF-8.
std::fs::pathtype struct IoError {
kind: IoErrorKind;
os_code: i32;
}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.
primitive ()The unit type: a value that carries no information, and the empty tuple. Distinct from void, which is the absence of a value - Result<(), E> succeeds with nothing to return. 0 bytes.
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.
IoErrorKind::AlreadyExistsfunction 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.
function rename(from: Path, to: Path) -> Result<(), IoError>Rename / move from to to. rename(2) (atomic within a filesystem; fails with EXDEV across filesystems).
(parameter) from: Path(parameter) to: Pathfunction 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.
type struct PathBuf<A = GlobalAlloc> {
inner: String<A>;
}function realpath(path: u8*, resolved: u8*) -> u8*