Skip to content

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.

export enum CStringFromPathError:
case InteriorNul
case ExportFailed
export def cstring_from_path(path: view Path) -> Result[CString, CStringFromPathError]
  • InteriorNul: the path contains a 0x00 byte and can’t be represented as a C string.
  • ExportFailed: the runtime couldn’t materialise the export (e.g., a working-set conversion failure).
NeedUse
Pass a literal to a C functionraw cstr "literal" (see Unsafe and FFI)
Convert a view str you controlCString.from_str(text): panics on interior NUL
Convert a user-provided view Pathcstring_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.