std::fs::dir
Directory Iteration
Owned iterator over directory entries with automatic cleanup.
Fs::ReadDir is an owned directory iterator that wraps DIR* and
automatically calls closedir when dropped. Each call to next()
returns an owned DirEntry with copied strings.
Example
import Fs from "std::fs";
import Io from "std::io";
function main(): i32 {
let result: Result<Fs::ReadDir, Io::IoError> = Fs::ReadDir::open("/tmp");
if (result.isError()) {
return 1;
}
let mut iter: Fs::ReadDir = result.unwrap();
loop {
match (iter.next()) {
Result::OK(Option::SOME(entry)) -> {
if (entry.isDir()) {
Io::println(entry.name);
}
},
Result::OK(Option::NONE) -> {
break;
},
Result::ERROR(_) -> {
return 1;
},
}
}
return 0;
}
namespace FsOwned directory entry.
Records
record DirEntryDirectory entry produced by ReadDir and filesystem walking helpers.
name: StringFile name (owned).
path: PathBufFull path: baseDir + “/” + name (owned).
fileType: FileTypeClassified file type.
ino: u64Inode number.
isDir(&self): booleanReturns true if this entry is a directory.
isFile(&self): booleanReturns true if this entry is a regular file.
isSymlink(&self): booleanReturns true if this entry is a symbolic link.
record ReadDirOwned directory iterator.
dir: *mut voidbaseDir: Stringstatic open(path: str): Result<ReadDir, IoError>Opens a directory for iteration.
close(&mut self): voidCloses the directory stream without consuming the iterator value.
drop(&mut self): voidCloses the directory stream.
next(&mut self): Result<Option<DirEntry>, IoError>Returns the next directory entry.
Result::OK(Option::NONE) means end of directory.
Functions
function collectWalk(path: str): Result<Vector<String>, IoError>Returns a depth-first list of paths rooted at path.
function collectWalkChildren(parent: &String, output: &mut Vector<String>): Result<void, IoError>Recursively collects children of parent without following symlink dirs.
function collectWalkDirectory(path: str, output: &mut Vector<String>): Result<void, IoError>Collects entries under an existing directory path.
function collectWalkEntry(path: &String, isDirectory: boolean, output: &mut Vector<String>): Result<void, IoError>Adds path to walk output and descends when it is a directory.
function collectWalkPath(path: str, output: &mut Vector<String>): Result<void, IoError>Walks a raw C-string path after copying it to owned storage.
function collectWalkPathOwned(path: &String, output: &mut Vector<String>): Result<void, IoError>Walks an owned path after classifying it with lstat.
function collectWalkSingle(path: str, output: &mut Vector<String>): Result<void, IoError>Collects a single non-directory path.
function isDotEntry(name: String): booleanReturns true for owned . and .. entry names.
function isDotEntryName(name: str): booleanReturns true for raw C-string . and .. entry names.
function isDotEntryPath(path: &PathBuf): booleanReturns true when a path’s final component is . or ...
function removeDirAllPath(path: str): Result<void, IoError>Removes a directory tree through the platform system layer.
function walkContains(entries: &Vector<String>, expected: str): booleanReturns whether a walk result contains expected exactly.
function walkDescribe(entries: &Vector<String>): StringJoins walk result entries with newlines for diagnostics and tests.
function walkEntryFromPath(path: PathBuf): Result<DirEntry, IoError>Builds a DirEntry for a path using lstat so symlinks stay leaves.