Skip to content
CryoCryo home
Stdlibcollections

string

import std::collections::string; · source

Types

String<A>

type struct String<A = GlobalAlloc> {
    buffer: RawBuffer<u8, A>;
    length: u64;

    static new() -> String;
    static new_in(alloc: A) -> String<A>;
    static with_capacity(capacity: u64) -> String;
    static try_with_capacity_in(capacity: u64, alloc: A) -> Result<String<A>, AllocError>;
    static from_str(source: Str) -> String;
    ![implicit]
    static from_string(text: string) -> String;
    static with_null(source: Str) -> String;
    static from<T>(item: T) -> String;
    push<T>(mut &this, item: T) -> void;
    try_push<T>(mut &this, item: T) -> Result<(), AllocError>;
    push_byte(mut &this, byte: u8) -> void;
    try_push_byte(mut &this, byte: u8) -> Result<(), AllocError>;
    clear(mut &this) -> void;
    length(&this) -> u64;
    capacity(&this) -> u64;
    is_empty(&this) -> boolean;
    as_str(&this) -> Str;
    as_bytes(&this) -> Slice<u8>;
    as_ptr(&this) -> u8*;
    as_cstr(mut &this) -> u8*;
    into_raw(mut this) -> string;
    starts_with(&this, prefix: &Str) -> boolean;
    ends_with(&this, suffix: &Str) -> boolean;
    find(&this, needle: &Str) -> Option<u64>;
    contains(&this, needle: &Str) -> boolean;
    trim(&this) -> Str;
    trim_start(&this) -> Str;
    trim_end(&this) -> Str;
    to_ascii_uppercase(&this) -> String;
    to_ascii_lowercase(&this) -> String;
    split(&this, sep: &Str) -> SplitIter;
    chars(&this) -> Chars;
    char_indices(&this) -> CharIndices;
    truncate(mut &this, new_length: u64) -> void;
    reserve(mut &this, additional: u64) -> Result<(), AllocError>;
    reserve_exact(mut &this, additional: u64) -> Result<(), AllocError>;
    shrink_to_fit(mut &this) -> void;
}

The owned, growable companion to Str: a heap byte buffer plus a length. No NUL terminator, no encoding assumptions beyond "the bytes are valid UTF-8". The first allocation reserves 8 bytes.

Properties

PropertyType
bufferRawBuffer<u8, A>
lengthu64

Constructors

static new() -> String;

Empty string. No heap storage until the first push_* / reserve.

static new_in(alloc: A) -> String<A>;
static with_capacity(capacity: u64) -> String;

Preallocate room for capacity bytes. Panics on allocation failure.

static try_with_capacity_in(capacity: u64, alloc: A) -> Result<String<A>, AllocError>;
static from_str(source: Str) -> String;

Construct from a Str by copying its bytes. Panics on allocation failure.

![implicit] static from_string(text: string) -> String;

Construct an owned String from a string (Cryo's raw NUL-terminated C-string view) by copying its bytes. Marked ![implicit]: the compiler treats this as the constructor when a string flows into a String slot - a parameter (println("text")) or a typed binding (mut s: String = "text"). The resulting String owns its buffer and moves into the destination. Named for its source type (string), the mirror of from_str (which takes a Str); with_null is the inverse direction (produce a C-facing NUL-terminated buffer).

static with_null(source: Str) -> String;

Construct a NUL-terminated copy of source for handing to C. The terminating NUL lives in the buffer (so as_ptr() yields a valid const char*) but is excluded from the logical length, so as_str() / length() see only source. Matches C semantics on an interior NUL: the C side stops reading at the first one. This is the produce-for-C direction; from_string is the consume-from-C one. Pairs with the as_cstr() borrow method.

static from<T>(item: T) -> String;

Create a String from item, using the SAME compile-time static match (T) dispatch as push - the central T-generic constructor. String::from("hi"), String::from(65 as u32), String::from(true), String::from(some_str) all route through push. Panics on allocation failure.

Methods

push<T>(mut &this, item: T) -> void;

Append item to the string as its text representation, dispatched at compile time via static match (T) - no runtime cost, no trait bound, because the available arms ARE the accepted types: - Str / string - the view's / C string's UTF-8 bytes verbatim (a string's terminating NUL is not copied). - every primitive integer - its decimal Display text, e.g. (u8..u128, i8..i128, s.push(65 as u8) appends "65". usize / isize) - f32 / f64 - its Display text. - boolean - "true" / "false". Any other type is a compile error (the static match has no _ arm). To

try_push<T>(mut &this, item: T) -> Result<(), AllocError>;

Fallible push: returns AllocError instead of panicking. Same compile-time dispatch as push.

push_byte(mut &this, byte: u8) -> void;

Append a single raw byte (not its decimal text). For byte-exact work: NUL terminators, control characters, binary buffers. The caller is responsible for keeping the string valid UTF-8. Panics on allocation failure; use try_push_byte for the fallible form.

try_push_byte(mut &this, byte: u8) -> Result<(), AllocError>;

Fallible push_byte: append one raw byte, returning AllocError instead of panicking.

clear(mut &this) -> void;
length(&this) -> u64;
capacity(&this) -> u64;
is_empty(&this) -> boolean;
as_str(&this) -> Str;

Borrow as a Str. Invalidated by the next mutating call.

as_bytes(&this) -> Slice<u8>;
as_ptr(&this) -> u8*;
as_cstr(mut &this) -> u8*;

Borrow this String as a NUL-terminated C string (u8* / const char*) for handing to an extern "C" API.

into_raw(mut this) -> string;

Consume the String and return its NUL-terminated buffer as a raw string (C-string view), leaking the allocation so the caller owns the bytes with no borrow lifetime. A NUL terminator is appended first. The owning companion of Str::sub_bytes etc. for handing an owned C-string across the FFI / raw-string boundary.

starts_with(&this, prefix: &Str) -> boolean;
ends_with(&this, suffix: &Str) -> boolean;
find(&this, needle: &Str) -> Option<u64>;
contains(&this, needle: &Str) -> boolean;
trim(&this) -> Str;

Borrowed sub-slice with surrounding ASCII whitespace removed.

trim_start(&this) -> Str;

Borrowed sub-slice with leading ASCII whitespace removed. Mirrors Str::trim_start, closing the String vs Str method asymmetry.

trim_end(&this) -> Str;

Borrowed sub-slice with trailing ASCII whitespace removed. Mirrors Str::trim_end.

to_ascii_uppercase(&this) -> String;
to_ascii_lowercase(&this) -> String;

Fresh String with every ASCII uppercase byte folded to lowercase. Mirrors Str::to_ascii_lowercase and the existing String::to_ascii_uppercase.

split(&this, sep: &Str) -> SplitIter;

Iterator over pieces separated by sep (borrows this String).

chars(&this) -> Chars;

Iterator over Unicode scalar values (u32), decoding UTF-8 on the fly. Borrows this String; see Str::chars.

char_indices(&this) -> CharIndices;

Iterator over (byte_offset, scalar) pairs. Borrows this String; see Str::char_indices.

truncate(mut &this, new_length: u64) -> void;

Shrink length to new_length. No reallocation; capacity is unchanged. Bytes beyond new_length are no longer considered part of the string; subsequent push_* calls will overwrite them.

reserve(mut &this, additional: u64) -> Result<(), AllocError>;
reserve_exact(mut &this, additional: u64) -> Result<(), AllocError>;
shrink_to_fit(mut &this) -> void;

Constructing

new and new_in allocate nothing until the first push; with_capacity preallocates, and try_with_capacity_in is its fallible form. from_string copies a C string and is marked ![implicit], so mut s: String = "text" and println("text") both route through it. from<T> is the general constructor — the same dispatch as push — and with_null keeps a trailing NUL for the FFI boundary.

Appending

push<T> appends item as its text representation, dispatched at compile time through static match (T) — no runtime cost and no trait bound, because the available arms are the accepted types:

  • String, Str, and string — the UTF-8 bytes verbatim (a string's terminating NUL is not copied).
  • Every primitive integer, usize, and isize — its decimal text. s.push(65 as u8) appends "65".
  • f32 and f64 — their Display text.
  • boolean — "true" or "false".

Anything else is a compile error; the static match has no _ arm.

mut s: String = String::new();
s.push("count: ");
s.push(42);            // "count: 42"
s.push_byte(0x0A);     // a raw newline byte

push_byte appends a single raw byte rather than its decimal text — for NUL terminators, control characters, and binary buffers. Keeping the result valid UTF-8 is your responsibility. Both have try_ forms returning Result<(), AllocError> instead of panicking.

Reading and borrowing

length(), capacity(), is_empty(), as_str() -> Str, as_bytes() -> Slice<u8>, as_ptr() -> u8*, plus clear(), truncate(new_length), reserve, reserve_exact, and shrink_to_fit.

The Str conveniences are forwarded so you don't write s.as_str().starts_with(...) everywhere: starts_with, ends_with, find, contains, trim, trim_start, trim_end, to_ascii_uppercase, to_ascii_lowercase, split, chars, and char_indices. Everything they return borrows this string's buffer and is valid only while it lives unmutated.

Crossing into C

A String built by push carries no trailing NUL, so as_ptr() alone is not a valid C string. Two methods bridge the gap:

  • as_cstr(mut &this) -> u8* borrows the buffer as a NUL-terminated C string. It writes the terminator at index length — reserving one spare byte if the buffer is full — without changing the logical length. Idempotent and zero-allocation once the spare byte exists. The pointer is valid until the next mutating call and must not be freed by the caller.
  • into_raw(mut this) -> string consumes the string, appends a terminator, and hands back the raw buffer, leaking the allocation so the caller owns the bytes outright.

As in C, an interior NUL ends the string early; the bytes are not otherwise validated.

Trait implementations

implement<A> trait Eq for struct String<A> where A: Allocator
implement<A> trait Hash for struct String<A> where A: Allocator
implement<A> trait Ord for struct String<A> where A: Allocator
implement<A> trait Drop for struct String<A> where A: Allocator
implement trait Clone for struct String<GlobalAlloc>
implement trait Default for struct String<GlobalAlloc>
implement<A> trait Display for struct String<A> where A: Allocator
std::fmt::display
implement trait Debug for struct String<GlobalAlloc>
std::fmt::display
implement trait FmtWrite for struct String<GlobalAlloc>
std::fmt::write

Eq, Ord, and Hash agree with Str's, so a String and a Str holding the same bytes hash and compare identically. FmtWrite is what lets a String be the sink of a Formatter — format_to_string is built on it.

Constants

const INITIAL_CAPACITY: u64 = 8;