String Literals
A string literal is the most common way a slice appears directly in source. Unlike an array literal [a, b, c] — which produces an owning array T[N] — a "..." literal produces a slice: a Slice<char8> whose data pointer addresses the string bytes stored in the read-only data section of the binary.
let greeting = "Hello"; // Slice<char8>; greeting.length == 5
Two details follow from this representation:
- Length is the byte count.
lengthcounts bytes, not characters, and does not include any terminator. - Strings are not null-terminated. Rux strings carry their length in the slice header, so there is no trailing
\0. When calling C, which expects null-terminated strings, account for this difference explicitly.
Because a string is a Slice<char8>, it supports the same indexing and iteration as any other slice, and it can be passed to any function that takes a Slice<char8>.
func Greet(name: Slice<char8>) {
Print("Hello, ", name);
}
Greet("Rux"); // the literal is a Slice<char8> slice into rodata
See Also
- Slices — the slice view type and its memory layout
- Arrays — where the
[a, b, c]literal form belongs - Character Types — the
char8element type behind strings - Indexing and Iteration — working with the characters of a string
Overview
A slice, written Slice<T>, is a view over a contiguous sequence of elements. It does not own the elements — it only records where they live and how many there are. The storage belongs to something else: an array, a heap allocation, or a static in the binary.
Indexing and Iteration
A slice reaches its elements exactly as an array does — by position or in a loop — because both share the same element access. What is unique to a slice is that the access goes through its data pointer into memory it does not own.