std::pathFile path and glob utilities.
Manipulate file paths (join, split, check existence) and expand glob patterns to lists of matching paths.
import std.path;
fn main() {
let dir = path.dirname("/home/user/file.txt");
let ext = path.extension("photo.jpg");
let result = match path.glob("*.hew") {
.Ok(result) => result,
.Err(error) => panic(to_string(error)),
};
for i in 0..result.len() {
println(result.get(i));
}
result.close();
}
globExpand a glob pattern and return all matching paths.
Returns an error when the expansion cannot complete (for example a matching directory that cannot be read).
import std.path;
fn main() {
let files = match path.glob("src/**/*.hew") {
.Ok(files) => files,
.Err(error) => panic(to_string(error)),
};
}
Expand a glob pattern, reporting expansion failure separately from a completed walk that matched nothing.
import std.path;
fn main() {
match path.glob("src/**/*.hew") {
.Ok(files) => println(f"{files.len()} match(es)"),
.Err(err) => println(f"{err}"),
}
}
combineCombine two path segments with the platform path separator.
import std.path;
fn main() {
println(path.combine("/home/user", "file.txt")); // "/home/user/file.txt"
}
dirnameReturn the directory portion of a path.
import std.path;
fn main() {
println(path.dirname("/home/user/file.txt")); // "/home/user"
}
basenameReturn the final component of a path.
import std.path;
fn main() {
println(path.basename("/home/user/file.txt")); // "file.txt"
}
extensionReturn the file extension (without the dot).
import std.path;
fn main() {
println(path.extension("photo.jpg")); // "jpg"
}
existsTest whether a path exists on the filesystem.
is_fileTest whether a path refers to a regular file.
is_dirTest whether a path refers to a directory.
absoluteReturn the absolute form of a path.
This is a lexical join against the current working directory: it does
NOT resolve .. or symlinks. absolute("../../etc/passwd") returns
<cwd>/../../etc/passwd, not a collapsed or containment-checked path.
Pass the result through normalize for a lexically clean path, or use
a real symlink-aware canonicalize (not yet in std) before treating a
path as safe to open — normalize alone does not guard against ..
escaping through a symlinked directory.
normalizeLexically clean a path: collapse repeated separators, drop . segments,
and resolve .. segments against the preceding component. Follows Go's
filepath.Clean semantics. Does not touch the filesystem — it has no
symlink awareness, so a cleaned path can still escape a directory the
caller intended to stay inside if any component is a symlink. Returns
"." for an empty path.
import std.path;
fn main() {
path.normalize("/var/www/../../etc/passwd"); // "/etc/passwd"
path.normalize("a/./b/../c"); // "a/c"
path.normalize("../../etc/passwd"); // "../../etc/passwd"
path.normalize(""); // "."
}
GlobResultPathErrorErrors produced by std.path glob expansion and indexing.
GlobFailed(string)The glob walk did not complete — an unreadable directory, an allocation failure, or a platform with no implementation.
IndexOutOfRange(string)A match index was negative or at/past the match count.
GlobResultMethodsMethods available on a GlobResult.
Return the number of matched paths.
Return the matched path at the given index.
Return the matched path at the given index, or None when the index
is outside 0 .. len().