The `path` module provides a unified way to work wil file paths across different host systems.
use fmt;
use path;
export fn main() void = {
// We can construct a path buffer with the segments of the path
// we want and not worry about the slashes since the buffer will
// handle this for us accordingly.
const pbuf = path::init("some", "path", "on", "the", "fs")!;
fmt::println(path::string(&pbuf))!;
// We can add more segments to the buffer as well.
const p = path::push(&pbuf, "down", "the", "rabbit", "hole")!;
fmt::println(p)!;
const popped = path::pop(&pbuf);
fmt::println(popped)!;
// We can overwrite the buffer with a completely new set of
// segments. The buffer also handles dots and slashes the right
// way.
const new = path::set(&pbuf, "some", "../", "path", "/again")!;
fmt::println(new)!;
const prepend = path::prepend(&pbuf, "add", "in", "front")!;
fmt::println(prepend)!;
// We can get the parent of the path.
const parent = path::parent(&pbuf)!;
fmt::println(parent)!;
// We an iterate over the buffer and pull the segments out one
// by one.
const iter = path::iter(&pbuf);
for (true) {
match (path::nextiter(&iter)) {
// We got a string segment so we can print it.
case let seg: str =>
fmt::println(seg)!;
// Iterator returned done so there is no more segments
// and in this case we break.
case done =>
break;
};
};
};
$ hare run file-paths.ha
4/4 tasks completed (100%)
some/path/on/the/fs
some/path/on/the/fs/down/the/rabbit/hole
hole
path/again
add/in/front/path/again
add/in/front/path
add
in
front
path
again
Back to table of contents