std.cstring
Checked CString construction over filesystem paths.
std.cstring exposes checked helpers for constructing CString values. The runtime also exposes constructors CString.from_str and CString.from_path directly, but those panic on interior NUL bytes: the helpers here surface the error as a Result.
Definition
Section titled “Definition”export enum CStringFromPathError: case InteriorNul case ExportFailed
export def cstring_from_path(path: view Path) -> Result[CString, CStringFromPathError]InteriorNul: the path contains a0x00byte and can’t be represented as a C string.ExportFailed: the runtime couldn’t materialise the export (e.g., a working-set conversion failure).
When to use which constructor
Section titled “When to use which constructor”| Need | Use |
|---|---|
| Pass a literal to a C function | raw cstr "literal" (see Unsafe and FFI) |
Convert a view str you control | CString.from_str(text): panics on interior NUL |
Convert a user-provided view Path | cstring_from_path(path): returns a Result |
For most filesystem code, the std.fs helpers (open_read, read_file_bytes, etc.) already do the right thing: you only reach for cstring_from_path when calling into a C API directly.