Strings
cx has two string types:
string, a reference to a sequence of characters, and the type of string literals.
Use it when you need to pass a string to a function without mutating it.
It borrows the characters without owning them.StringBuf, a buffer storing a sequence of characters.
Use it when you need to build dynamic strings.
It owns its characters and grows as needed.
void main() {
var a = "test";
// a has type 'string'
var b = StringBuf(a);
// b holds a copy of "test" that we can mutate
b.push('!');
// b now holds "test!", a is unchanged
println(a); // prints "test"
println(b); // prints "test!"
}A StringBuf converts to a string view
implicitly, so buffers pass directly to functions taking strings:
int stringSize(string s) {
return s.size();
}
void main() {
var buffer = StringBuf("hello");
println(stringSize(buffer)); // prints 5
}Interpolation
{expr} embeds a value or an arbitrary expression in a
string literal. Interpolated values must implement
Printable. Write \{ for a literal brace.
void main() {
var name = "world";
var greeting = "hello {name}!";
println(greeting); // prints "hello world!"
var math = "1 + 2 = {1 + 2}";
println(math); // prints "1 + 2 = 3"
println("\{name}"); // prints "{name}"
}