std/string

March 14, 2026 ยท View on GitHub

String is a growable, heap-allocated string type. It wraps a Vec<char> and ensures null-termination for C compatibility.

Usage

import "std/string.zc"

fn main() {
    let s = String::from("Hello");

    // Append requires a pointer to another String
    let part = String::from(" World");
    s.append(&part);
    
    // Iteration (UTF-8 aware)
    for c in s {
        println "{c}";
    }

    // Use c_str() to print
    println "{s.c_str()}"; // Prints "Hello World"
    
    if (s.starts_with("Hello")) {
        // ...
    }
} // s is freed automatically here

Struct Definition

struct String {
    vec: Vec<char>;
}

Methods

Construction

MethodSignatureDescription
newString::new(s: char*) -> StringCreates a new String from a C string primitive.
fromString::from(s: char*) -> StringAlias for new.
from_runeString::from_rune(r: rune) -> StringCreates a new String from a single rune.
from_runesString::from_runes(runes: rune*, count: usize) -> StringCreates a new String from an array of runes.
from_runes_vecString::from_runes_vec(runes: Vec<rune>) -> StringCreates a new String from a vector of rune objects.

Modification

MethodSignatureDescription
appendappend(self, other: String*)Appends another string to this one.
append_cappend_c(self, s: char*)Appends a C string literal.
push_runepush_rune(self, r: rune)Appends a single Unicode code point (rune) to the string.
insert_runeinsert_rune(self, idx: usize, r: rune)Inserts a rune at the specified character index.
remove_rune_atremove_rune_at(self, idx: usize) -> runeRemoves and returns the rune at the specified character index.
reservereserve(self, cap: usize)Ensures the string has at least cap characters of capacity.
clearclear(self)Clears the string.

Access & Query

MethodSignatureDescription
c_strc_str(self) -> char*Returns the underlying C string pointer.
lengthlength(self) -> usizeReturns the length of the string (excluding null terminator).
is_emptyis_empty(self) -> boolReturns true if length is 0.
to_stringto_string(self) -> char*Maps to c_str(). Used for {var} interpolation.
starts_withstarts_with(self, prefix: char*) -> boolChecks if the string starts with the given prefix.
ends_withends_with(self, suffix: char*) -> boolChecks if the string ends with the given suffix.
containscontains(self, target: char) -> boolChecks if the string contains the given character.
contains_strcontains_str(self, target: char*) -> boolChecks if the string contains the given substring.
findfind(self, target: char) -> Option<usize>Returns the index of the first occurrence of byte target.
find_strfind_str(self, target: char*) -> Option<usize>Returns the index of the first occurrence of substring target.
find_all_strfind_all_str(self, target: char*) -> Vec<usize>Returns a vector containing all indices where target occurs.
substringsubstring(self, start: usize, len: usize) -> StringReturns a new String containing the specified substring.

UTF-8 Support

MethodSignatureDescription
utf8_lenutf8_len(self) -> usizeReturns the number of Unicode code points (characters).
utf8_atutf8_at(self, idx: usize) -> StringReturns the character at the specified index as a new String.
utf8_getutf8_get(self, idx: usize) -> runeReturns the character at the specified index as a rune.
utf8_substrutf8_substr(self, start_idx: usize, num_chars: usize) -> StringReturns a substring based on character indices.
runesrunes(self) -> Vec<rune>Returns a vector containing all Unicode code points.
charschars(self) -> StringCharsIterReturns a manual iterator yielding Option<rune>.

Transformations

MethodSignatureDescription
to_lowercaseto_lowercase(self) -> StringReturns a new string converted into lowercase.
to_uppercaseto_uppercase(self) -> StringReturns a new string converted into uppercase.
splitsplit(self, delim: char) -> Vec<String>Splits the string into a vector of substrings.
trimtrim(self) -> StringReturns a new string with leading/trailing whitespace removed.
replacereplace(self, target: char*, replacement: char*) -> StringReturns a new string with replacements.
pad_leftpad_left(self, target_len: usize, pad_char: char) -> StringReturns a new string padded on the left.
pad_rightpad_right(self, target_len: usize, pad_char: char) -> StringReturns a new string padded on the right.

Comparison

MethodSignatureDescription
eqeq(self, other: String*) -> boolStructural equality check.
neqneq(self, other: String*) -> boolStructural inequality check.
comparecompare(self, other: String*) -> intLexical comparison.
compare_ignore_casecompare_ignore_case(self, other: String*) -> intCase-insensitive lexical comparison.
eq_ignore_caseeq_ignore_case(self, other: String*) -> boolCase-insensitive equality check.

Operators

OperatorMethodDescription
+adds1 + &s2. Concatenates strings into a new String.
+=add_assigns1 += &s2. Appends s2 to s1 in place.
==eqs1 == &s2. structural equality check.
!=neqs1 != &s2. structural inequality check.
<lts1 < &s2. Lexical comparison.
>gts1 > &s2. Lexical comparison.
<=les1 <= &s2. Lexical comparison.
>=ges1 >= &s2. Lexical comparison.
{}to_stringUsed for string interpolation in printf/println.

Iteration

MethodSignatureDescription
iteratoriterator(self) -> StringCharsIterReturns an iterator yielding rune. Used by for c in s.

Memory Management

MethodSignatureDescription
freefree(self)Manually frees the string memory.
destroydestroy(self)Alias for free.
forgetforget(self)Prevents automatic freeing (transfer ownership).
Traitimpl Drop for StringAutomatically calls free() when out of scope.