data Path
Text operations on /-separated paths; nothing touches the file system.
Example:
use std.io { Path }
fn main() io {
let p = Path.of("/var/log") / "app.log"
print("{p.show()} {p.isAbsolute()}")
}Output
/var/log/app.log true
Examples#
Recipes for paths: parts of a path, joining, normalization.
Parts of a path and joining#
Path does not touch the file system: it is pure string work (fileName, stem, extension, parent, normalize, the / operator).
use std.io { Path }
fn main() io {
let p = Path.of("/srv/app/config/prod.toml")
print("{p.fileName()} {p.stem()} {p.extension()}") // name, name without extension, extension
print("{p.parent() ?? Path.of("/")}")
print("{p.withExtension("json")}")
print("{p.isAbsolute()} {Path.of("rel/x").isAbsolute()}")
let joined = Path.of("/srv") / "app" / "logs" // the / operator joins segments
print("{joined}")
print("{Path.of("/a/b/../c/./d").normalize()}")
print("{p.components()}")
}Output
prod.toml prod toml /srv/app/config /srv/app/config/prod.json true false /srv/app/logs /a/c/d ["srv", "app", "config", "prod.toml"]
@derive(Eq, Hash)
pub(api) data PathThread safety. an immutable value.
Since. 1.0.0-beta
Fields
pub(api) text: str | The path as text. |
Methods
of | (text: str) -> PathThe path with this text. |
join | (other: str) -> PathPath.of("a/b").join("c") is a/b/c; an absolute right side replaces the left; empty parts are ignored. |
/ | (a: Path, b: str) -> PathJoins a path and a segment with / (an absolute b replaces a). |
isAbsolute | () -> boolWhether the path starts with /. |
fileName | () -> strLast component (a/b.txt -> b.txt; / and the empty path -> empty). |
parent | () -> Path?Everything before the last component (a/b/c -> a/b, a -> empty, /a -> /), none for / and the empty path. |
extension | () -> strExtension of the file name without the dot (a.tar.gz -> gz); empty if none; .bashrc has none. |
stem | () -> strFile name without the extension (a.tar.gz -> a.tar). |
withExtension | (ext: str) -> PathThe path with its extension replaced by ext (added when there is none). |
components | () -> List<str>Non-empty components (/a//b/ -> a, b). |
normalize | () -> PathRemoves . components and folds .. |
show | () -> strThe path as text. |
toString | () -> strThe path as text. |
Defined in: std/io/io.oxl:826