path is a module — import path — and path.Path is what a file is named
with. On Unix a name is any bytes but NUL, and café.txt written in Latin-1 is
as real a file as the same name in UTF-8. A string is UTF-8 by construction,
so it cannot hold the first name and a Path can. fs
takes a Path wherever it takes a name, and answers one wherever it names a
file: every fs.Entry and every fs.Error carries its path as a Path.
A Path is one block of bytes. Taking it apart (parent, file_name,
extension, each component) answers &Path windows over that block and
allocates nothing: each borrows the path, so it cannot outlive it or be read
past a change to it, and .clone() makes a Path of its own. Only join and
push write bytes, into one block sized for the answer. Comparison, hashing
and order are by the bytes.
From a string, and from bytes
A string is already a path, so fs takes one as it is: fs.File.read("a.txt")
needs no conversion and copies nothing. Path.from moves a string’s block into
a Path, and so does into: s.into() gives the receiver away, so a string
nothing reads afterwards becomes the path without a copy. Path.from_bytes takes
any bytes, and to_str goes back to text, saying where the bytes stop being text
when they do.
import path
fn main() void {
text := path.Path.from("café.txt")
raw := path.Path.from_bytes([0x63u8, 0x61u8, 0x66u8, 0xE9u8])
println("${text} ${raw}")
println(raw.debug_string())
match raw.to_str() {
Ok(s) => println("text: ${s}")
Err(e) => println("not text after ${e.valid_up_to} bytes")
}
}Display is for a person: it writes the text and U+FFFD for each run of bytes
that is not text. Debug is exact: it quotes the path and writes each byte that
is not text as \xNN. as_bytes and to_str are the exact forms to compute
with.
Taking a path apart
The rules are Rust’s std::path::Path on Unix, which the tests compare against
on a generated set: / separates names, runs of / are one separator, a .
name is dropped unless it comes first, and a trailing / names nothing.
| Method | Answers |
|---|---|
p.parent() |
&Path? — the path without its last component; None for the root and the empty path |
p.file_name() |
&Path? — the last name; None for the root and for a path ending in .. |
p.file_stem() |
&Path? — the file name up to its last . |
p.extension() |
&Path? — what follows the last .; None for .rc |
p.has_extension("gz") |
bool — the extension is exactly that |
p.starts_with(base), p.ends_with(child) |
bool — base’s components lead the path’s, or child’s end them: /etc/passwd starts with /etc and not /e |
p.strip_prefix(base) |
&Path? — the path less base’s leading components; None where they do not lead it |
p.components() |
the iterator of path.Component: RootDir, CurDir, ParentDir, Normal(name) |
p.is_absolute(), p.is_relative() |
bool — whether the first byte is / |
import path
fn main() void {
p := path.Path.from("/home/ada/notes.tar.gz")
println("${p.parent() ?? p}")
println("${p.file_name() ?? p}")
println("${p.file_stem() ?? p}")
println("${p.extension() ?? p}")
for c in p.components() {
match c {
RootDir => println("root")
CurDir => println("here")
ParentDir => println("up")
Normal(name) => println("name ${name}")
}
}
}Nothing here asks the disk. parent of a/.. is a, and whether that is the
directory above is the file system’s to say, because a symbolic link may be in
the way. Components compare whole, so strip_prefix and the two tests read
/srv/data and /srv//data/ as one path, and /srv/da as another:
import path
fn main() void {
p := path.Path.from("/srv/data/2026/report.csv")
println("${p.starts_with("/srv/data")} ${p.starts_with("/srv/da")} ${p.ends_with("2026/report.csv")}")
match p.strip_prefix("/srv//data/") {
Some(rest) => println("${rest}")
None => println("elsewhere")
}
}Joining
join answers a new path, and push appends in place. An absolute path
replaces what it is joined onto, any other follows one / (none after a /, none
after the empty path), and the answer is written into one block of exactly its
length. Either takes a Path or a string.
import path
fn main() void {
base := path.Path.from("/srv/data")
println("${base.join("2026/report.csv")}")
println("${base.join("/etc/hosts")}")
mut out := path.Path.with_capacity(32)
out.push("/srv")
out.push("data")
out.push("x.csv")
println("${out}")
}with_extension and with_file_name answer a new path with the file name’s
extension, or the file name, replaced, as Rust’s do: the empty extension
removes one, and a path with no file name takes the name joined on.
import path
fn main() void {
p := path.Path.from("/srv/data/report.csv")
println("${p.with_extension("json")}")
println("${p.with_extension("")}")
println("${p.with_file_name("summary.txt")}")
}What takes a path
fs.File and fs.Dir take anything that is path.AsPath: a string, a
Path, or a type of your own that implements it. The operations are generic over
the name only to take it as a Path, so a string costs a window over its own
bytes and the code of an operation is the same whichever way a name was written.
An operation that only reads a name borrows it, as above. A function that keeps the
path takes it with Into[path.Path] instead, which a string and a Path both
satisfy, and moves it into place with into. A borrowed name converted to a Path
before the call would be a count and a header the call does not need, which is why
fs does not ask for one.
import path
struct Library {
root path.Path
}
fn library[P](root P) Library where P: Into[path.Path] {
return Library { root: root.into() }
}
fn main() void {
a := library("/srv/books")
b := library(path.Path.from("/srv/more"))
println("${a.root} ${b.root}")
}import fs
import path
fn names(dir path.Path) string ! fs.Error {
mut out := ""
for e in fs.Dir.list(dir)? {
out.append("${e.name} ")
}
return out
}
fn main() void {
match names(path.Path.from("/etc")) {
Ok(listing) => println("${listing.len() > 0}")
Err(e) => println("could not list: ${e}")
}
}Availability
path has no starts_with, strip_prefix, with_extension or canonicalize
yet, and no Windows prefixes or \ separator: a Path reads the bytes as Unix
does. A host whose paths are not bytes refuses a name that is not UTF-8 with
InvalidPath; the types are unchanged.