Skip to content
CryoCryo home
Stdlibcollections

str

import std::collections::str; · source

Types

Str

type struct Str {
    bytes:  u8*;
    length: u64;

    ![implicit]
    static new(text: string) -> Str;
    static from_raw(bytes: u8*, length: u64) -> Str;
    static from<T>(src: T) -> Str;
    length(&this) -> u64;
    is_empty(&this) -> boolean;
    as_bytes(&this) -> Slice<u8>;
    as_ptr(&this) -> u8*;
    byte_at(&this, index: u64) -> Option<u8>;
    sub_bytes(&this, start: u64, end: u64) -> Str;
    to_ascii_lowercase(&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_start(&this) -> Str;
    trim_end(&this) -> Str;
    trim(&this) -> Str;
    to_ascii_uppercase(&this) -> String;
    split(&this, sep: &Str) -> implement Iterator<Str>;
    chars(&this) -> implement Iterator<u32>;
    char_indices(&this) -> implement Iterator<Pair<u64, u32>>;
    try_to_hex(&this) -> Result<u64, ConversionError>;
}

A borrowed UTF-8 slice: (bytes, length) over memory the caller guarantees outlives the view. There is no NUL terminator — the length is authoritative.

new is marked ![implicit], so a string literal flowing into a Str slot converts on its own; the NUL is not part of the view. from<T> dispatches at compile time over string (length via strlen) and Slice<u8> (borrowed verbatim). from_raw is the escape hatch for a pointer you already have a length for.

length() counts bytes, not scalars, and find returns a byte offset. The trim* family strips ASCII whitespace only, and to_ascii_lowercase / to_ascii_uppercase allocate a String. chars() decodes scalars, yielding U+FFFD for malformed input; char_indices() pairs each with its byte offset.

The three iterators are concrete public types — SplitIter, Chars, and CharIndices — each implementing Iterator, so they can be named in a field or returned from your own functions.

Properties

PropertyType
bytesu8*
lengthu64

Constructors

![implicit] static new(text: string) -> Str;

Construct from a NUL-terminated C-style string literal. The caller certifies that text is valid UTF-8 and outlives this view. The NUL terminator is not included in the view.

static from_raw(bytes: u8*, length: u64) -> Str;

Construct from a raw pointer + length. The caller certifies that [bytes, bytes + length) is initialized UTF-8 and outlives this view.

static from<T>(src: T) -> Str;

Construct a Str view from a text source, dispatched on T at compile time: - string - a NUL-terminated C-string (literal or an i8*/u8* cast to string); length via strlen, the NUL is not part of the view. - Slice<u8> - an existing byte slice, borrowed verbatim. The caller certifies the bytes are valid UTF-8 and outlive the view. For a raw pointer + length use from_raw; the ![implicit] new remains the implicit string -> Str conversion.

Methods

length(&this) -> u64;

Byte length. For a UTF-8 string, length() >= char_count.

is_empty(&this) -> boolean;
as_bytes(&this) -> Slice<u8>;

Underlying byte view. Use this for byte-granularity algorithms (hashing, equality, substring search).

as_ptr(&this) -> u8*;

Raw byte pointer. Valid as long as this Str is valid.

byte_at(&this, index: u64) -> Option<u8>;

Bounds-checked byte lookup. For scalar iteration, use as_bytes() with a UTF-8 decoder.

sub_bytes(&this, start: u64, end: u64) -> Str;

[start, end) byte sub-slice. Panics if the range is out of bounds or if start > end. Not UTF-8-aware; splitting inside a multi-byte scalar is the caller's responsibility.

to_ascii_lowercase(&this) -> String;

Return a new String with every ASCII uppercase byte folded to lowercase. Non-ASCII bytes are passed through unchanged. Requires collections::string; callers that only need Str can byte-scan manually, but most callers want this method.

starts_with(&this, prefix: &Str) -> boolean;

True if this string begins with prefix (byte-wise).

ends_with(&this, suffix: &Str) -> boolean;

True if this string ends with suffix (byte-wise).

find(&this, needle: &Str) -> Option<u64>;

Byte index of the first occurrence of needle, or None. An empty needle matches at index 0. O(n*m) naive scan; inputs in practice are short. Not UTF-8-aware (matches on bytes).

contains(&this, needle: &Str) -> boolean;

True if needle appears anywhere in this string.

trim_start(&this) -> Str;

Borrowed sub-slice with leading ASCII whitespace removed.

trim_end(&this) -> Str;

Borrowed sub-slice with trailing ASCII whitespace removed.

trim(&this) -> Str;

Borrowed sub-slice with leading and trailing ASCII whitespace removed. ASCII whitespace is space, tab, LF, VT, FF, CR.

to_ascii_uppercase(&this) -> String;

Return a new String with every ASCII lowercase byte folded to uppercase. Non-ASCII bytes pass through unchanged. Mirror of to_ascii_lowercase.

split(&this, sep: &Str) -> implement Iterator<Str>;

Iterator over the (non-overlapping) pieces between occurrences of the byte separator sep. A leading/trailing/doubled separator yields an empty piece. Each yielded Str borrows from this one. split on a one-byte separator is the common case; multi-byte separators are supported too.

chars(&this) -> implement Iterator<u32>;

Iterator over the Unicode scalar values of this string, decoding UTF-8 on the fly. Each scalar is yielded as a u32 in the range [0, 0x10FFFF] (Cryo's char is a single byte, so a full scalar is a u32, not a char). This is the scalar-level counterpart to byte_at/as_bytes, which stay byte-level.

char_indices(&this) -> implement Iterator<Pair<u64, u32>>;

Iterator over (byte_offset, scalar) pairs: the same scalars as chars, each paired with the byte index at which it begins. The pair is Pair<u64, u32> (p.first is the byte offset, p.second the scalar). Byte offsets are into this view's bytes, so a four-byte scalar advances the next offset by four.

try_to_hex(&this) -> Result<u64, ConversionError>;

Parsing numbers

TryFrom<Str> is implemented for every integer width, which is how you parse text:

import std::collections::str;
import std::core::convert;

match (u32::try_from(arg)) {
    Result::Ok(n)  => { /* parsed */ }
    Result::Err(e) => { eprintln(f"not a number: {e}"); }
}

Trait implementations

implement trait Eq for struct Str
implement trait Hash for struct Str
implement trait Ord for struct Str
implement trait Display for struct Str
std::fmt::display
implement trait Debug for struct Str
std::fmt::display
implement trait TryFrom<&JsonValue> for Str
std::json::value

TryFrom<Str> for every integer width is what backs number parsing (the list also shows the numeric narrowing conversions from core::convert, which share the trait):

implement trait TryFrom<Str> for u64
implement trait TryFrom<Str> for u32
implement trait TryFrom<Str> for u16
implement trait TryFrom<Str> for u8
implement trait TryFrom<Str> for i64
implement trait TryFrom<Str> for i32
implement trait TryFrom<Str> for i16
implement trait TryFrom<Str> for i8
implement trait TryFrom<i32> for i16
std::core::convert
implement trait TryFrom<i64> for i32
std::core::convert
implement trait TryFrom<u64> for u32
std::core::convert
implement trait TryFrom<i64> for u64
std::core::convert
implement trait TryFrom<u64> for i64
std::core::convert
implement trait TryFrom<&JsonValue> for boolean
std::json::value
implement trait TryFrom<&JsonValue> for i64
std::json::value
implement trait TryFrom<&JsonValue> for u64
std::json::value
implement trait TryFrom<&JsonValue> for f64
std::json::value
implement trait TryFrom<&JsonValue> for Str
std::json::value

Ord is lexicographic over bytes. Eq plus Hash is what makes Str usable as a HashMap key. Debug renders the text quoted and escaped.

Constants

const REPLACEMENT_SCALAR: u32 = 0xFFFD;
const I64_MIN_MAGNITUDE:  u64 = 9223372036854775808;