std/path

March 14, 2026 ยท View on GitHub

The std/path module provides cross-platform utilities for manipulating file system paths. It simplifies common tasks like joining paths, extracting extensions, and finding parent directories.

Overview

  • Cross-platform: Handles both forward and backward slashes appropriately during manipulation.
  • Type-safe: The Path struct encapsulates path information, distinguishing it from regular strings.
  • Convenient Parsing: Easily extract components like extension, file_name, and parent.
  • RAII: Memory is automatically managed via the Drop trait.

Usage

import "std/path.zc"

fn main() {
    let p = Path::new("/home/user");
    let full_path = p.join("docs/file.txt");
    
    println "Full path: {full_path.c_str()}";
    
    match full_path.extension() {
        Some(ext) => println "Extension: {ext}",
        None => println "No extension found"
    }
} // full_path and p are freed automatically here

Struct Definition

struct Path {
    str: String;
}

Methods

Construction

MethodSignatureDescription
newPath::new(s: char*) -> PathCreates a new Path from a C string.
from_stringPath::from_string(s: String) -> PathCreates a Path by taking ownership of a String.
cloneclone(self) -> PathReturns a deep copy of the Path.

Manipulation

MethodSignatureDescription
joinjoin(self, other: char*) -> PathAppends other to the path using the correct directory separator.

Parsing

MethodSignatureDescription
extensionextension(self) -> Option<String>Returns the file extension (without the leading dot), if any.
file_namefile_name(self) -> Option<String>Returns the final component of the path.
parentparent(self) -> Option<Path>Returns the parent directory path.

Access

MethodSignatureDescription
c_strc_str(self) -> char*Returns the underlying C string representation.

Memory Management

MethodSignatureDescription
freefree(self)Manually frees the path's internal string memory.
Traitimpl Drop for PathAutomatically calls free() when out of scope.