path
import std::fs::path; · source
Both Path and PathBuf are length-typed UTF-8 byte sequences with no NUL terminator. Path borrows; PathBuf owns. Crossing into libc goes through a NUL-terminated String built with String::with_null, or PathBuf::into_raw, which hands the buffer over with a terminator appended.
Path syntax is per-target. On Unix / separates and a leading / means absolute — the whole path is one flat sequence of segments. On Windows both / and \ separate, and a path may open with a root that is not a segment and must never be split off or reordered:
C:\dir\file drive-absolute root `C:\`
C:dir drive-relative root `C:`, resolved against that drive's own cwd
\\server\share UNC
Types
Path
type struct Path {
inner: Str;
static from(source: Str) -> Path;
as_str(&this) -> Str;
length(&this) -> u64;
is_empty(&this) -> boolean;
static is_separator(byte: u8) -> boolean;
static byte_or_zero(source: Str, index: u64) -> u8;
static is_drive_letter(byte: u8) -> boolean;
static root_len(source: Str) -> u64;
static is_absolute_str(source: Str) -> boolean;
static last_separator(source: Str, floor: u64) -> i64;
static last_dot(name: Str) -> i64;
is_absolute(&this) -> boolean;
file_name(&this) -> Option<Str>;
parent(&this) -> Option<Path>;
extension(&this) -> Option<Str>;
join(&this, segment: Str) -> PathBuf;
}
root_len, is_separator, and is_absolute_str are the target-aware primitives the methods are built on, exposed for code that has a Str in hand and no reason to wrap it.
file_name and parent are purely lexical — they never consult the filesystem — and both return None for an empty path or a bare root, where a rooted path's parent yields the root itself, never "". join appends to a copy as a PathBuf, leaving the borrowed path untouched.
extension() consults only the file name, so a dot in a directory component cannot leak in:
foo.rs -> Some("rs")
foo.tar.gz -> Some("gz") last dot only
foo. -> Some("") trailing dot, empty extension
foo -> None no dot
.bashrc -> None a leading dot is not an extension
.. -> None
a/b -> None ends in a directory
A leading dot only disqualifies when it is the only dot — .foo.rs still has extension rs.
Properties
| Property | Type |
|---|---|
inner | Str |
Constructors
static from(source: Str) -> Path;
static from(source: Str) -> Path;Borrow a Str as a Path. A NUL-terminated string (C-string / i8*) converts implicitly at the call site via Str's ![implicit] new, so Path::from("/tmp") and Path::from(some_str) both work; a C-string's terminating NUL is not part of the path bytes. The borrowed bytes must outlive the Path.
Static functions
static is_separator(byte: u8) -> boolean;
static is_separator(byte: u8) -> boolean;Whether byte separates segments on this target.
static byte_or_zero(source: Str, index: u64) -> u8;
static byte_or_zero(source: Str, index: u64) -> u8;source's byte at index, or 0 when out of range. Path syntax never gives 0 a meaning, so callers can compare it without unwrapping.
static is_drive_letter(byte: u8) -> boolean;
static root_len(source: Str) -> u64;
static root_len(source: Str) -> u64;Length of source's leading root, including its trailing separator when one is present; 0 when the path is fully relative.
static is_absolute_str(source: Str) -> boolean;
static is_absolute_str(source: Str) -> boolean;Whether source resolves without reference to a working DIRECTORY.
static last_separator(source: Str, floor: u64) -> i64;
static last_separator(source: Str, floor: u64) -> i64;Index of the last separator at or after floor, or -1 when there is none. Searching from floor is what keeps the root's own separators from being mistaken for segment breaks. Signed so "none" stays distinct from "found at floor" - both are reachable, and conflating them is what makes a root look like an ordinary segment.
static last_dot(name: Str) -> i64;
static last_dot(name: Str) -> i64;Index of the last . in name, or -1 when there is none. name is a single path component, not a whole path, so this never has to skip a root or separators - the caller passes the output of file_name.
Methods
as_str(&this) -> Str;
length(&this) -> u64;
is_empty(&this) -> boolean;
is_absolute(&this) -> boolean;
is_absolute(&this) -> boolean;Whether this path is absolute; see is_absolute_str.
file_name(&this) -> Option<Str>;
file_name(&this) -> Option<Str>;Last path segment, or None if the path ends with a separator, is empty, or is nothing but a root. Does not consult the filesystem.
parent(&this) -> Option<Path>;
parent(&this) -> Option<Path>;Path minus its final segment, or None if there is no parent - a single relative segment, a bare root, or empty. A rooted path whose last segment is stripped yields the root itself, never "".
extension(&this) -> Option<Str>;
extension(&this) -> Option<Str>;Extension of the final component: the bytes after its last ., or None. It consults only the file name - parent().extension() cannot leak in a dot from a directory:
join(&this, segment: Str) -> PathBuf;
join(&this, segment: Str) -> PathBuf;Append segment to a COPY of this path, leaving the borrowed path untouched. The non-mutating twin of PathBuf::push, and it inherits push's root semantics: a segment carrying its own root - /etc, C:\dir, \\server\share - replaces rather than extends: join("/etc") is /etc whatever the path was.
PathBuf<A>
type struct PathBuf<A = GlobalAlloc> {
inner: String<A>;
static new() -> PathBuf;
static from(source: Str) -> PathBuf;
as_path(&this) -> Path;
length(&this) -> u64;
is_empty(&this) -> boolean;
push(mut &this, segment: Str) -> void;
pop(mut &this) -> boolean;
join(&this, segment: Str) -> PathBuf;
extension(&this) -> Option<Str>;
into_raw(mut this) -> string;
}
push and join share their root semantics: a segment carrying its own root — /etc, C:\dir, \\server\share — replaces rather than extends. pop removes the last segment and returns whether anything was removed.
Properties
| Property | Type |
|---|---|
inner | String<A> |
Constructors
static new() -> PathBuf;
static from(source: Str) -> PathBuf;
static from(source: Str) -> PathBuf;Own a copy of a Str (or, implicitly, a NUL-terminated string via Str's ![implicit] new). The bytes are copied into the owned buffer, so source need not outlive the result.
Methods
as_path(&this) -> Path;
length(&this) -> u64;
is_empty(&this) -> boolean;
push(mut &this, segment: Str) -> void;
push(mut &this, segment: Str) -> void;Append segment, inserting a separator if needed. A segment carrying a root of its own - /etc, C:\dir, \dir, \\server\share - replaces the current path rather than extending it, since appending one path's root inside another would name nothing.
pop(mut &this) -> boolean;
pop(mut &this) -> boolean;Remove the last segment. Returns true if anything was popped; false at a bare root or an empty path, so this cannot climb out of the volume the path names.
join(&this, segment: Str) -> PathBuf;
join(&this, segment: Str) -> PathBuf;Append segment to a COPY of this path and return it, leaving self intact. push mutates in place; join is the owning, non-mutating twin. Same root semantics as push (a rooted segment replaces).
extension(&this) -> Option<Str>;
extension(&this) -> Option<Str>;Extension of the final component; see Path::extension.
into_raw(mut this) -> string;
into_raw(mut this) -> string;Consume the PathBuf and return its bytes as an owned, NUL-terminated raw string, leaking the allocation so the caller owns it with no borrow lifetime. Mirrors String::into_raw for handing an owned C-string across the raw-string boundary.
Trait implementations
implement<A> trait Drop for struct PathBuf<A> where A: Allocator
Path is a borrowed view and has nothing to drop.
Constants
const SEPARATOR: u8 = 0x2F;
const BACKSLASH: u8 = 0x5C;
const COLON: u8 = 0x3A;
const DOT: u8 = 0x2E;
![target(windows)]
const WINDOWS_SYNTAX: boolean = true;
![target(unix)]
const WINDOWS_SYNTAX: boolean = false;type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct Path {
inner: Str;
}Path / PathBuf: filesystem paths as length-typed UTF-8.
Path.inner: Strtype struct Str {
bytes: u8*;
length: u64;
}Str: a borrowed UTF-8 string slice.
std::collections::strstatic name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
static Path::from(source: Str) -> PathBorrow a Str as a Path. A NUL-terminated string (C-string / i8*) converts implicitly at the call site via Str's ![implicit] new, so Path::from("/tmp") and Path::from(some_str) both work; a C-string's terminating NUL is not part of the path bytes. The borrowed bytes must outlive the Path.
(parameter) source: Strtype struct Path {
inner: Str;
}Path / PathBuf: filesystem paths as length-typed UTF-8.
std::fs::pathPath::as_str(&this) -> Str&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
Path::length(&this) -> u64Path::is_empty(&this) -> booleanstatic Path::is_separator(byte: u8) -> booleanWhether byte separates segments on this target.
(parameter) byte: u8static Path::byte_or_zero(source: Str, index: u64) -> u8source's byte at index, or 0 when out of range. Path syntax never gives 0 a meaning, so callers can compare it without unwrapping.
(parameter) index: u64static Path::is_drive_letter(byte: u8) -> booleanstatic Path::root_len(source: Str) -> u64Length of source's leading root, including its trailing separator when one is present; 0 when the path is fully relative.
static Path::is_absolute_str(source: Str) -> booleanWhether source resolves without reference to a working DIRECTORY.
static Path::last_separator(source: Str, floor: u64) -> i64Index of the last separator at or after floor, or -1 when there is none. Searching from floor is what keeps the root's own separators from being mistaken for segment breaks.
(parameter) floor: u64static Path::last_dot(name: Str) -> i64Index of the last . in name, or -1 when there is none. name is a single path component, not a whole path, so this never has to skip a root or separators - the caller passes the output of file_name.
(parameter) name: StrPath::is_absolute(&this) -> booleanWhether this path is absolute; see is_absolute_str.
Path::file_name(&this) -> Option<Str>Last path segment, or None if the path ends with a separator, is empty, or is nothing but a root. Does not consult the filesystem.
Path::parent(&this) -> Option<Path>Path minus its final segment, or None if there is no parent - a single relative segment, a bare root, or empty. A rooted path whose last segment is stripped yields the root itself, never "".
Path::extension(&this) -> Option<Str>Extension of the final component: the bytes after its last ., or None. It consults only the file name - parent().extension() cannot leak in a dot from a directory:
Path::join(&this, segment: Str) -> PathBufAppend segment to a COPY of this path, leaving the borrowed path untouched. The non-mutating twin of PathBuf::push, and it inherits push's root semantics: a segment carrying its own root - /etc, C:\dir, \\server\share - replaces rather than extends: join("/etc") is /etc whatever the path was.
(parameter) segment: Strtype struct PathBuf<A = GlobalAlloc> {
inner: String<A>;
}type struct PathBuf<A = GlobalAlloc> {
inner: String<A>;
}type parameter Atype struct GlobalAlloc {}Zero-sized handle to the process-wide allocator. Construct with GlobalAlloc::new(); all instances dispatch through the same underlying backend.
PathBuf.inner: String<A>type struct String<A = GlobalAlloc> {
buffer: RawBuffer<u8, A>;
length: u64;
}String: owned, heap-allocated, length-typed UTF-8.
std::collections::stringtype parameter Astatic PathBuf::new() -> PathBufstatic PathBuf::from(source: Str) -> PathBufOwn a copy of a Str (or, implicitly, a NUL-terminated string via Str's ![implicit] new). The bytes are copied into the owned buffer, so source need not outlive the result.
PathBuf::as_path(&this) -> PathPathBuf::length(&this) -> u64PathBuf::is_empty(&this) -> booleanPathBuf::push(mut &this, segment: Str) -> voidAppend segment, inserting a separator if needed. A segment carrying a root of its own - /etc, C:\dir, \dir, \\server\share - replaces the current path rather than extending it, since appending one path's root inside another would name nothing.
mut &this
mut &TypeAn exclusive, mutating borrow. As a receiver, mut &this lets the method modify the fields - and callers can see that from the signature alone. As a parameter type, mut &T lets the callee mutate through the reference while the caller keeps ownership.
PathBuf::pop(mut &this) -> booleanRemove the last segment. Returns true if anything was popped; false at a bare root or an empty path, so this cannot climb out of the volume the path names.
PathBuf::join(&this, segment: Str) -> PathBufAppend segment to a COPY of this path and return it, leaving self intact. push mutates in place; join is the owning, non-mutating twin. Same root semantics as push (a rooted segment replaces).
PathBuf::extension(&this) -> Option<Str>Extension of the final component; see Path::extension.
PathBuf::into_raw(mut this) -> stringConsume the PathBuf and return its bytes as an owned, NUL-terminated raw string, leaking the allocation so the caller owns it with no borrow lifetime. Mirrors String::into_raw for handing an owned C-string across the raw-string boundary.
mut thisA by-value receiver the body may also reassign. The receiver is moved into the method and consumed; the caller's value may not be used afterwards.
language referenceprimitive stringNUL-terminated raw string (u8*), matching the C ABI. Length-typed text is Str / String.
const NAME: Type = value;At module level, a true compile-time constant, conventionally SCREAMING_SNAKE_CASE. (mut at module level declares mutable global state instead.)
const SEPARATOR: u8 = 0x2F;const BACKSLASH: u8 = 0x5C;const COLON: u8 = 0x3A;const DOT: u8 = 0x2E;const WINDOWS_SYNTAX: boolean = true;trueThe boolean literal. Booleans are a distinct 1-byte type, not interchangeable with integers; if and while conditions must be boolean.
falseThe boolean literal. Booleans are a distinct 1-byte type, not interchangeable with integers; if and while conditions must be boolean.
import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
static String::with_null(source: Str) -> StringConstruct a NUL-terminated copy of source for handing to C. The terminating NUL lives in the buffer (so as_ptr() yields a valid const char*) but is excluded from the logical length, so as_str() / length() see only source. Matches C semantics on an interior NUL: the C side stops reading at the first one.
PathBuf::into_raw(mut this) -> stringConsume the PathBuf and return its bytes as an owned, NUL-terminated raw string, leaking the allocation so the caller owns it with no borrow lifetime. Mirrors String::into_raw for handing an owned C-string across the raw-string boundary.
static Path::root_len(source: Str) -> u64Length of source's leading root, including its trailing separator when one is present; 0 when the path is fully relative.
static Path::is_separator(byte: u8) -> booleanWhether byte separates segments on this target.
static Path::is_absolute_str(source: Str) -> booleanWhether source resolves without reference to a working DIRECTORY.
Path::file_name(&this) -> Option<Str>Last path segment, or None if the path ends with a separator, is empty, or is nothing but a root. Does not consult the filesystem.
Path::parent(&this) -> Option<Path>Path minus its final segment, or None if there is no parent - a single relative segment, a bare root, or empty. A rooted path whose last segment is stripped yields the root itself, never "".
Option::NonePath::join(&this, segment: Str) -> PathBufAppend segment to a COPY of this path, leaving the borrowed path untouched. The non-mutating twin of PathBuf::push, and it inherits push's root semantics: a segment carrying its own root - /etc, C:\dir, \\server\share - replaces rather than extends: join("/etc") is /etc whatever the path was.
Path::extension(&this) -> Option<Str>Extension of the final component: the bytes after its last ., or None. It consults only the file name - parent().extension() cannot leak in a dot from a directory:
Path.inner: Strstatic Path::from(source: Str) -> PathBorrow a Str as a Path. A NUL-terminated string (C-string / i8*) converts implicitly at the call site via Str's ![implicit] new, so Path::from("/tmp") and Path::from(some_str) both work; a C-string's terminating NUL is not part of the path bytes. The borrowed bytes must outlive the Path.
new T { field: value }
new E::Variant(args)
new T[n]Heap allocation. new T { .. } places a struct literal on the heap and yields a T*, and new E::V(args) does the same for an enum variant; new T[n] reserves n uninitialised T - the typed malloc. The storage comes from T's Allocate implementation, or else the global allocator. Parentheses after a struct's name are refused (E0117): there are no constructors, and a struct's static new(..) is an ordinary method.
static Path::byte_or_zero(source: Str, index: u64) -> u8source's byte at index, or 0 when out of range. Path syntax never gives 0 a meaning, so callers can compare it without unwrapping.
static Path::is_drive_letter(byte: u8) -> booleanstatic Path::last_separator(source: Str, floor: u64) -> i64Index of the last separator at or after floor, or -1 when there is none. Searching from floor is what keeps the root's own separators from being mistaken for segment breaks.
static Path::last_dot(name: Str) -> i64Index of the last . in name, or -1 when there is none. name is a single path component, not a whole path, so this never has to skip a root or separators - the caller passes the output of file_name.
Path::as_str(&this) -> StrPath::length(&this) -> u64Path::is_empty(&this) -> booleanPathBuf::push(mut &this, segment: Str) -> voidAppend segment, inserting a separator if needed. A segment carrying a root of its own - /etc, C:\dir, \dir, \\server\share - replaces the current path rather than extending it, since appending one path's root inside another would name nothing.
PathBuf::pop(mut &this) -> booleanRemove the last segment. Returns true if anything was popped; false at a bare root or an empty path, so this cannot climb out of the volume the path names.
static PathBuf::new() -> PathBufPathBuf::as_path(&this) -> PathString::into_raw(mut this) -> stringConsume the String and return its NUL-terminated buffer as a raw string (C-string view), leaking the allocation so the caller owns the bytes with no borrow lifetime. A NUL terminator is appended first. The owning companion of Str::sub_bytes etc. for handing an owned C-string across the FFI / raw-string boundary.
implement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
type parameter Awhere A: Allocator
type trait Name : Base {
method(&this, other: &This) -> Ret;
}Names a set of methods a type may implement. Inside the body This is the implementing type; methods may carry default bodies, and a trait may require a base trait (type trait Ord : Eq). Generic code asks for a trait with where T: Name.
implement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language referencewhere T: Ord + Clone, U: HashConstrains generic parameters to types that implement the listed traits, which is what makes those traits' methods callable on T. Bounds on one parameter join with +; parameters separate with ,. A where on a single method narrows the type's parameter for that method only.
type trait Allocator {
allocate(mut &this, layout: Layout) -> Result<NonNull<u8>, AllocError>;
deallocate(mut &this, ptr: NonNull<u8>, layout: Layout) -> void;
reallocate(mut &this, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout) -> Result<NonNull<u8>, AllocError>;
}Allocator: the trait every heap strategy implements.
std::alloc::allocator