Skip to content

std/cstr ​

A safe, non-owning view of a NUL-terminated C string (*u8). CStr.wrap answers None for a null pointer, byte is bounds-checked, and the view never frees the memory: the bytes belong to whoever produced the pointer.

milo
from "std/cstr" import { CStr }

Example ​

milo
let s = "hello"
// CStr.wrap is @unsafe: the pointer must be NUL-terminated and outlive the CStr.
unsafe {
    match CStr.wrap(s.cstr()) {
        Some(c) => print(c.len()),   // 5
        None => print("null"),
    }
}

API reference ​

CStr ​

milo
pub struct CStr

@copy: a non-owning view. The bytes belong to whoever produced the pointer (C, or a Milo string's buffer), so two CStr values over them are two readers, not two owners.

CStr.byte ​

milo
fn CStr.byte(self: &CStr, i: i64): u8

Bounds-checked byte access. Panics if i >= len.

CStr.eq ​

milo
fn CStr.eq(self: &CStr, other: CStr): bool

CStr.len ​

milo
fn CStr.len(self: &CStr): i64

CStr.ptr ​

milo
fn CStr.ptr(self: &CStr): *u8

CStr.toString ​

milo
fn CStr.toString(self: &CStr): string

CStr.wrap ​

milo
fn CStr.wrap(ptr: *u8): Option<CStr>