Skip to content

Paths (std::path)

std::path is a string-manipulation library; it does not touch the disk. Every function receives and returns path strings, using the correct OS separator. To check whether a path exists or read its content, use std::fs.


Join, split, and extract components

path.join assembles paths with the native separator. path.dirname and path.basename split the path at the last separator. path.ext and path.stem isolate the extension and the name without extension:

Joins parts with join; extracts directory, basename, extension, and stem.

01-join-split.zolo
Playground
// Feature: path.join / path.dirname / path.basename — path manipulation
// When to use: build cross-platform paths without worrying about / vs \.

use std::path

// path.join(...) glues parts together using the right OS separator.
let p = path.join("home", "user", "docs", "file.txt")
print(p)  // expected: home/user/docs/file.txt (or \ on Windows)

// path.dirname -> everything up to the last separator.
print(path.dirname("/var/log/app.log"))  // expected: /var/log

// path.basename -> last component of the path.
print(path.basename("/var/log/app.log"))  // expected: app.log

// path.ext / path.stem split off the extension.
print(path.ext("report.pdf"))  // expected: .pdf  (or pdf — depends on impl)
print(path.stem("report.pdf"))  // expected: report

// path.separator is "/" on Unix and "\" on Windows.
print(path.separator())  // expected: OS separator

Absolute, relative, and normalization

path.is_absolute / path.is_relative query the kind of path. path.normalize collapses . and .. and duplicate separators. path.resolve returns the absolute path (using the current working directory if the path is relative). path.relative(from, to) computes the relative path between two points:

Checks type, normalizes, resolves, and computes relative paths.

02-absolute-relative.zolo
Playground
// Feature: path.is_absolute / is_relative / normalize / resolve / relative
// When to use: validate inputs, resolve relative paths, deduplicate.

use std::path

// is_absolute / is_relative — query the kind of path.
print(path.is_absolute("/etc/hosts"))  // expected: true (false on Windows)
print(path.is_relative("docs/readme"))  // expected: true

// normalize collapses "." and ".." and duplicate separators.
print(path.normalize("a/b/../c/./d"))  // expected: a/c/d

// resolve returns the absolute path (joins with cwd if relative).
let abs = path.resolve("report.txt")
print(abs.len() > 0)  // expected: true

// relative(from, to) computes the relative path from `from` to `to`.
print(path.relative("/var/log", "/var/log/app/error.log"))
// expected: app/error.log

Combining path with fs to check existence

The existence functions (exists, is_file, is_dir) belong to std::fs, not to std::path. This example shows how to build a path with path.join and test it with fs:

Joins components with path.join and checks existence with fs.exists / fs.is_file.

03-exists-via-fs.zolo
// Feature: path/fs — test for file existence

// When to use: validate user input, decide whether to create a file.

// Note: IO functions (`exists`, `is_file`, `is_dir`) live in the `fs` module,

// not in `path`. `path` is pure string manipulation; `fs` touches the disk.


use std::path
use std::fs

// Build the path with path.join and test with fs.exists.

let target = path.join("examples", "features", "17-stdlib", "path", "README.md")

// fs.exists(path) -> bool.

let here = fs.exists(target)
print(here)  // expected: true (when running from project root)


// Other type checks:

//   fs.is_file(p)   — regular file

//   fs.is_dir(p)    — directory

print(fs.is_file(target))  // expected: true

print(fs.is_dir("examples/features"))  // expected: true

Requires the Zolo CLI/host — open in the playground or run locally.

enespt-br