Skip to content
volt
GitHub

std::string

An owned, growable UTF-8 string.

Source: std/string.volt

Types

struct std::string

<Allocator: std::mem::allocator = std::mem::default_allocator> struct string

Owned, growable text: bytes (UTF-8 by convention, not checked). Prints as its text. An export fn that returns one hands other languages its text (@export_text).

Field
bytes: std::vec<u8, Allocator> = {}the text's bytes

Methods

<A: std::mem::allocator> attach fn to_json(this: std::string<A>&) -> std::json::value
<A: std::mem::allocator = std::mem::default_allocator> attach fn from(static this: std::string, s: str, allocator: A = {}) -> std::string<A>

a string holding a copy of s, in memory from allocator

<A: std::mem::allocator> attach fn new_in(static this: std::string, allocator: A) -> std::string<A>

an empty string that allocates from allocator

<A: std::mem::allocator> attach fn reserve(this: std::string<A>&, n: usize) -> std::mem::mem_error!void

room for n bytes without growing

<A: std::mem::allocator> attach fn as_str(this: std::string<A>&) -> str

the text (valid until the string changes)

<A: std::mem::allocator> attach fn len(this: std::string<A>&) -> usize

the length in bytes

<A: std::mem::allocator> attach operator [](this: std::string<A>&, i: usize) -> u8&

s[i]: byte i (not character i), a place, bounds-checked like the vec it's kept in

<A: std::mem::allocator> attach fn push(this: std::string<A>&, byte: u8) -> void

append one byte

<A: std::mem::allocator> attach fn append(this: std::string<A>&, s: str) -> void

append s (s can be part of this string: vec.extend finds it again if growing moves it). Inlined: out of line it took the string's address, so a caller building text kept the string in memory and reloaded it after every byte it pushed

<A: std::mem::allocator> attach fn append_int(this: std::string<A>&, v: i64) -> void

decimal digits of v

<A: std::mem::allocator> attach fn append_uint(this: std::string<A>&, v: u64) -> void

decimal digits of v (unchecked: a u64 has at most 20 digits, and the room for them was reserved)

<A: std::mem::allocator> attach fn is_empty(this: std::string<A>&) -> bool

is it empty?

<A: std::mem::allocator> attach fn clear(this: std::string<A>&) -> void

make it empty (the memory stays for reuse)

<A: std::mem::allocator> attach fn truncate(this: std::string<A>&, n: usize) -> void

keep the first n bytes (nothing happens when it's already that short)

<A: std::mem::allocator> attach fn pop(this: std::string<A>&) -> u8?

the last byte, taken off

<A: std::mem::allocator> attach fn insert(this: std::string<A>&, at: usize, s: str) -> void

s inserted at byte index at (the end when at is past it)

<A: std::mem::allocator> attach fn push_char(this: std::string<A>&, c: u32) -> void

append a character, as UTF-8 (one that isn't a character is U+FFFD)

<A: std::mem::allocator, B: std::mem::allocator> attach fn eq(this: std::string<A>&, other: std::string<B>&) -> bool

the same text

<A: std::mem::allocator, B: std::mem::allocator> attach fn cmp(this: std::string<A>&, other: std::string<B>&) -> i32

-1, 0 or 1: how the text sorts, byte by byte

<A: std::mem::allocator> attach fn hash(this: std::string<A>&) -> u64

the text's hash, so strings can be map keys

<A: std::mem::allocator> attach fn write_str(this: std::string<A>&, s: str) -> void

append s: this makes a string a writer, for std::write (and std::format)

<A: std::mem::allocator> attach fn c_str(this: std::string<A>&) -> cstr

a NUL-terminated view for C (valid until the string changes)