Module std::path

File path and glob utilities.

Manipulate file paths (join, split, check existence) and expand glob patterns to lists of matching paths.

Examples

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();
}

Contents

Functions

Function glob

pub fn glob(pattern: string) -> Result<GlobResult, PathError>

Expand 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).

Examples

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.

Examples

import std.path;

fn main() {
    match path.glob("src/**/*.hew") {
        .Ok(files) => println(f"{files.len()} match(es)"),
        .Err(err) => println(f"{err}"),
    }
}

Function combine

pub fn combine(a: string, b: string) -> string

Combine two path segments with the platform path separator.

Examples

import std.path;

fn main() {
    println(path.combine("/home/user", "file.txt"));   // "/home/user/file.txt"
}

Function dirname

pub fn dirname(p: string) -> string

Return the directory portion of a path.

Examples

import std.path;

fn main() {
    println(path.dirname("/home/user/file.txt"));   // "/home/user"
}

Function basename

pub fn basename(p: string) -> string

Return the final component of a path.

Examples

import std.path;

fn main() {
    println(path.basename("/home/user/file.txt"));   // "file.txt"
}

Function extension

pub fn extension(p: string) -> string

Return the file extension (without the dot).

Examples

import std.path;

fn main() {
    println(path.extension("photo.jpg"));   // "jpg"
}

Function exists

pub fn exists(p: string) -> bool

Test whether a path exists on the filesystem.

Function is_file

pub fn is_file(p: string) -> bool

Test whether a path refers to a regular file.

Function is_dir

pub fn is_dir(p: string) -> bool

Test whether a path refers to a directory.

Function absolute

pub fn absolute(p: string) -> string

Return 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.

Function normalize

pub fn normalize(p: string) -> string

Lexically 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.

Examples

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("");                           // "."
}

Types

Struct GlobResult

Fields

handle: GlobResultHandle

Enum PathError

Errors produced by std.path glob expansion and indexing.

Variants

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.

Traits

Trait GlobResultMethods

Methods available on a GlobResult.

Methods

fn len(self: Self) -> i64

Return the number of matched paths.

fn get(self: Self, index: i64) -> string

Return the matched path at the given index.

fn try_get(self: Self, index: i64) -> Option<string>

Return the matched path at the given index, or None when the index is outside 0 .. len().