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
| Property | Type |
|---|---|
bytes | u8* |
length | u64 |
Constructors
![implicit] static new(text: string) -> Str;
![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;
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;
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;
length(&this) -> u64;Byte length. For a UTF-8 string, length() >= char_count.
is_empty(&this) -> boolean;
as_bytes(&this) -> Slice<u8>;
as_bytes(&this) -> Slice<u8>;Underlying byte view. Use this for byte-granularity algorithms (hashing, equality, substring search).
as_ptr(&this) -> u8*;
as_ptr(&this) -> u8*;Raw byte pointer. Valid as long as this Str is valid.
byte_at(&this, index: u64) -> Option<u8>;
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;
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;
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;
starts_with(&this, prefix: &Str) -> boolean;True if this string begins with prefix (byte-wise).
ends_with(&this, suffix: &Str) -> boolean;
ends_with(&this, suffix: &Str) -> boolean;True if this string ends with suffix (byte-wise).
find(&this, needle: &Str) -> Option<u64>;
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;
contains(&this, needle: &Str) -> boolean;True if needle appears anywhere in this string.
trim_start(&this) -> Str;
trim_start(&this) -> Str;Borrowed sub-slice with leading ASCII whitespace removed.
trim_end(&this) -> Str;
trim_end(&this) -> Str;Borrowed sub-slice with trailing ASCII whitespace removed.
trim(&this) -> Str;
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;
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>;
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>;
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>>;
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;type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct Str {
bytes: u8*;
length: u64;
}Str: a borrowed UTF-8 string slice.
Str.bytes: u8*Str.length: u64static name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
static Str::new(text: string) -> StrConstruct 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.
(parameter) text: stringprimitive stringNUL-terminated raw string (u8*), matching the C ABI. Length-typed text is Str / String.
type struct Str {
bytes: u8*;
length: u64;
}Str: a borrowed UTF-8 string slice.
std::collections::strstatic Str::from_raw(bytes: u8*, length: u64) -> StrConstruct from a raw pointer + length. The caller certifies that [bytes, bytes + length) is initialized UTF-8 and outlives this view.
(parameter) bytes: u8*(parameter) length: u64// reservedA reserved keyword with no syntax of its own in the current grammar. The standard library uses it freely as a method and parameter name (Str::from, from: u64).
Str.src: T&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
Str::is_empty(&this) -> booleanStr::as_bytes(&this) -> Slice<u8>Underlying byte view. Use this for byte-granularity algorithms (hashing, equality, substring search).
type struct Slice<T> {
ptr: T*;
length: u64;
}Slice<T>: a borrowed view into a contiguous sequence.
std::core::sliceStr::as_ptr(&this) -> u8*Raw byte pointer. Valid as long as this Str is valid.
Str::byte_at(&this, index: u64) -> Option<u8>Bounds-checked byte lookup. For scalar iteration, use as_bytes() with a UTF-8 decoder.
(parameter) index: u64Str::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.
(parameter) start: u64(parameter) end: u64Str::to_ascii_lowercase(&this) -> StringReturn 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.
type struct String<A = GlobalAlloc> {
buffer: RawBuffer<u8, A>;
length: u64;
}String: owned, heap-allocated, length-typed UTF-8.
std::collections::stringStr::starts_with(&this, prefix: &Str) -> booleanTrue if this string begins with prefix (byte-wise).
(parameter) prefix: &StrStr::ends_with(&this, suffix: &Str) -> booleanTrue if this string ends with suffix (byte-wise).
(parameter) suffix: &StrStr::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).
(parameter) needle: &StrStr::contains(&this, needle: &Str) -> booleanTrue if needle appears anywhere in this string.
Str::trim_start(&this) -> StrBorrowed sub-slice with leading ASCII whitespace removed.
Str::trim_end(&this) -> StrBorrowed sub-slice with trailing ASCII whitespace removed.
Str::trim(&this) -> StrBorrowed sub-slice with leading and trailing ASCII whitespace removed. ASCII whitespace is space, tab, LF, VT, FF, CR.
Str::to_ascii_uppercase(&this) -> StringReturn a new String with every ASCII lowercase byte folded to uppercase. Non-ASCII bytes pass through unchanged. Mirror of to_ascii_lowercase.
Str::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.
(parameter) sep: &Str-> implement Trait<T>An opaque type: some one concrete type that implements the trait, without naming it. The compiler infers the real type and uses it everywhere; callers see only the trait's methods. How the stdlib returns iterators without exposing their engines.
language referencetype trait Iterator {
type Item;
next(mut &this) -> Option<This::Item>;
count(mut &this) -> u64;
fold<Acc>(mut &this, initial: Acc, f: (Acc, This::Item) -> Acc) -> Acc;
for_each(mut &this, f: (This::Item) -> void) -> void;
take(this, n: u64) -> TakeIter<This>;
skip(this, n: u64) -> SkipIter<This>;
map<B>(this, f: (This::Item) -> B) -> MapIter<This, B>;
filter(this, pred: (&This::Item) -> boolean) -> FilterIter<This>;
chain<J>(this, other: J) -> ChainIter<This, J>;
// … 7 more
}Str::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.
Str::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.
type struct Pair<A, B> {
first: A;
second: B;
}Pair<A, B>: an owned two-element tuple.
std::collections::pairStr::try_to_hex(&this) -> Result<u64, ConversionError>type struct ConversionError {
reason: string;
}What went wrong in a TryFrom / TryInto. Carries a short static message describing the failure; richer diagnostics are a caller concern.
import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
match (subject) {
Pattern(x) => { ... }
_ => { ... }
}The primary discriminator: branches on enum variants, integers, ranges, and destructuring patterns, and the compiler rejects a match that misses a case. There is no fallthrough. As an expression it evaluates to the chosen arm's value, so every arm must produce the same type.
static u32::try_from(source: Str) -> Result<u32, ConversionError>via trait TryFrom
std::collections::strResult::Ok(T)n: u32pattern => { body }Separates a match arm's pattern from its body. Arms do not fall through.
Result::Err(E)e: ConversionErrore: ConversionErrorconst NAME: Type = value;At module level, a true compile-time constant, conventionally SCREAMING_SNAKE_CASE. (mut at module level declares mutable global state instead.)
const REPLACEMENT_SCALAR: u32 = 0xFFFD;const I64_MIN_MAGNITUDE: u64 = 9223372036854775808;Str.bytes: u8*Str.length: u64new T { field: value }
new E::Variant(args)
new T[n]Heap allocation. new T { .. } places a struct literal on the heap and yields a T*, and new E::V(args) does the same for an enum variant; new T[n] reserves n uninitialised T - the typed malloc. The storage comes from T's Allocate implementation, or else the global allocator. Parentheses after a struct's name are refused (E0117): there are no constructors, and a struct's static new(..) is an ordinary method.
static Str::from_raw(bytes: u8*, length: u64) -> StrConstruct from a raw pointer + length. The caller certifies that [bytes, bytes + length) is initialized UTF-8 and outlives this view.
Str::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).
Str::trim(&this) -> StrBorrowed sub-slice with leading and trailing ASCII whitespace removed. ASCII whitespace is space, tab, LF, VT, FF, CR.
std::collections::strStr::to_ascii_lowercase(&this) -> StringReturn 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.
Str::to_ascii_uppercase(&this) -> StringReturn a new String with every ASCII lowercase byte folded to uppercase. Non-ASCII bytes pass through unchanged. Mirror of to_ascii_lowercase.
Str::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.
Str::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.
type struct SplitIter {
bytes: u8*;
length: u64;
sep_bytes: u8*;
sep_length: u64;
pos: u64;
done: boolean;
}Lazy iterator over the pieces of a Str separated by a byte sequence. Yields borrowed Str views; see Str::split. Borrows from the source Str, which must outlive the iterator.
type struct Chars {
bytes: u8*;
length: u64;
pos: u64;
}Lazy UTF-8 scalar iterator over a Str; see Str::chars. Borrows from the source Str, which must outlive the iterator.
type struct CharIndices {
bytes: u8*;
length: u64;
pos: u64;
}Lazy (byte_offset, scalar) iterator over a Str; see Str::char_indices. Borrows from the source Str. Public for the same cross-module reason as Chars.
static Str::new(text: string) -> StrConstruct 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.
(parameter) src: TStr::is_empty(&this) -> booleanStr::as_bytes(&this) -> Slice<u8>Underlying byte view. Use this for byte-granularity algorithms (hashing, equality, substring search).
std::collections::strStr::byte_at(&this, index: u64) -> Option<u8>Bounds-checked byte lookup. For scalar iteration, use as_bytes() with a UTF-8 decoder.
Str::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.
Str::starts_with(&this, prefix: &Str) -> booleanTrue if this string begins with prefix (byte-wise).
Str::ends_with(&this, suffix: &Str) -> booleanTrue if this string ends with suffix (byte-wise).
Option::NoneStr::contains(&this, needle: &Str) -> booleanTrue if needle appears anywhere in this string.
Str::trim_start(&this) -> StrBorrowed sub-slice with leading ASCII whitespace removed.
std::collections::strStr::trim_end(&this) -> StrBorrowed sub-slice with trailing ASCII whitespace removed.
std::collections::strStr::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.
Str::try_to_hex(&this) -> Result<u64, ConversionError>type trait TryFrom<T> {
static try_from(value: T) -> Result<This, ConversionError>;
}implement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
implement trait Trait for Type { ... }Introduces a trait implementation: the block supplies, for one concrete type, the method bodies the trait requires.
language referencetype trait Eq {
equals(&this, other: &This) -> boolean;
}Types that support value equality. equals(a, b) must be reflexive, symmetric, and transitive.
implement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language referencetype trait Ord : Eq {
compare(&this, other: &This) -> Ordering;
}Types that have a total order: every pair is Less, Equal, or Greater. compare must be consistent with equals: if a.equals(b) then a.compare(b) == Equal.
type trait Display {
fmt<W>(&this, f: mut &Formatter<W>) -> Result<(), FmtError>
where W: FmtWrite;
}Display / Debug / Formatter.
std::fmt::displaytype trait Debug {
fmt<W>(&this, f: mut &Formatter<W>) -> Result<(), FmtError>
where W: FmtWrite;
}type enum JsonValue {
Null;
Bool(boolean);
Number(JsonNumber);
String(String);
Array(Array<JsonValue>);
Object(JsonObject);
}One JSON value. Recursive through Array and Object.
type struct HashMap<K, V, A = GlobalAlloc> where A: Allocator {
buckets: Entry<K, V>**;
bucket_count: u64;
length: u64;
alloc: A;
seed: u64;
}HashMap<K, V, A>: key-value map with amortized O(1) operations.
std::collections::hashmap