Strings, Bytes, and Runes
Immutable UTF-8 strings, byte buffers, Unicode scalar values, and C strings.
Contents
str is an immutable UTF-8 string, bytes a mutable byte buffer, cstr a null-terminated C string, and rune a Unicode scalar value.
Strings#
A str is an immutable sequence of UTF-8 bytes. len is the number of bytes, not scalar values.
const text = "aé日"
print(text.len) // 5 bytes
print(text[0]) // 97, the first byte
print(text + "!")
Indexing yields a u8. Bounds are checked at runtime. Strings cannot be mutated. Build a new string with str::slice, concatenation, or a byte buffer.
const prefix = str::slice(text, 0, 1)
const rest = str::slice(text, 1, text.len)
print(prefix + rest)
str::slice(text, start, end) uses byte offsets; start is inclusive and end exclusive. Slicing inside a multi-byte sequence can produce invalid UTF-8.
Runes#
A rune is a Unicode scalar value. It is nominal and cannot be used as an integer without conversion. There is no rune literal syntax.
match rune::from_u32(0x263A) {
Success(smiley) => print(rune::to_u32(smiley))
Error(err) => print("invalid scalar value")
}
rune::from_u32 returns Result[rune, Utf8Error] and rejects surrogate code points and values above the Unicode maximum. rune::to_u32 returns the value as u32. print on a rune emits its numeric value; interpolation renders the character, and the u format shows the decimal scalar value.
Rune-Aware String Operations#
| Operation | Result | Meaning |
|---|---|---|
str::rune_count(text) |
Result[u64, Utf8Error] |
Number of scalar values, rejecting invalid UTF-8 |
str::rune_count_lossy(text) |
u64 |
Number of scalar values, replacing invalid bytes |
str::rune_at(text, index) |
Result[rune, Utf8Error] |
Scalar value at a scalar index |
str::runes(text) |
Result[List[rune], Utf8Error] |
All scalar values, rejecting invalid UTF-8 |
str::runes_lossy(text) |
List[rune] |
All scalar values, replacing invalid bytes |
str::slice_runes(text, start, end) |
Result[str, Utf8Error] |
Substring by scalar index |
str::slice_runes_lossy(text, start, end) |
str |
Substring by scalar index, replacing invalid bytes |
Strict forms report Utf8Error.InvalidEncoding for malformed input and Utf8Error.OutOfBounds for an out-of-range index. Lossy forms substitute replacement bytes.
match str::runes("aé日") {
Success(values) => print(values.len)
Error(err) => print("invalid UTF-8")
}
Byte Buffers#
A bytes value is a mutable buffer of unsigned bytes. Indexing reads or writes a u8; len is the current length as u64.
var buffer = bytes::new(4)
buffer[0] = 65
buffer[1] = 66
buffer[2] = 67
buffer[3] = 68
print(buffer.len)
buffer = bytes::append(buffer, 69)
print(bytes::to_str(buffer))
bytes::from_str, bytes::to_str, bytes::slice, and bytes::ptr convert and expose the buffer. Conversion to str does not validate UTF-8.
var copy = bytes::from_str("hello")
copy[0] = 72
print(bytes::to_str(copy))
print(bytes::to_str(bytes::slice(copy, 1, 4)))
bytes is a managed value. Mutation through one binding is visible through any binding referring to the same buffer.
C Strings#
A cstr is a pointer to a null-terminated byte sequence in unmanaged memory. It exists for C calls and operating system boundaries. It is not traced by the collector.
extern (C) puts(text cstr) i32 = "puts"
fn main() {
puts(cstr::from_str("hello from C"))
}
| Operation | Result |
|---|---|
cstr::from_str(text) |
A cstr view of a Haze string |
cstr::from_ptr(pointer) |
A cstr from a raw pointer |
cstr::to_str(value) |
Result[str, CStrError] |
cstr::to_bytes(value) |
Result[bytes, CStrError] |
cstr::is_null(value) |
bool |
cstr::null() |
The null cstr |
CStrError has variants NullPointer and InvalidUtf8. A null pointer cannot be converted to a string. Invalid UTF-8 is reported rather than replaced.
match cstr::to_str(cstr::from_str("world")) {
Success(text) => print(text)
Error(err) => print("conversion failed")
}
Raw Pointers#
ptr is an opaque raw pointer. It supports equality and a small set of pointer operations. It is managed as a root value but points to unmanaged memory; the collector does not trace through it.
const null_pointer = ptr::null()
print(null_pointer == ptr::null())
const advanced = ptr::add(null_pointer, 8)
Use ptr only when a C API requires an opaque handle or address. The boundary types are cstr, bytes::ptr, and extern records.
Choosing a Representation#
Use str for text that does not need mutation. Use bytes for binary data, incremental construction, or a buffer converted to text at the end. Use rune when reasoning about Unicode scalar values instead of bytes. Use cstr only in extern declarations or at the point of conversion from a C API. See Extern and C FFI.