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's likestd::string_viewin C++ and&strin Rust.StringBuf, a buffer storing a sequence of characters.
Use it when you need to build dynamic strings.
It's likestd::stringin C++ andStringin Rust.
void main() {
var a = "test";
// a has type 'string'
var b = StringBuf(a);
// b holds a copy of "test" that we can mutate
b.append('!');
// 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
$name embeds a value and ${expr} an
arbitrary expression in a string literal. Interpolated values must
implement Printable. Write $$ for a literal
dollar sign.
void main() {
var name = "world";
println("hello $name!"); // prints "hello world!"
println("1 + 2 = ${1 + 2}"); // prints "1 + 2 = 3"
println("$$5"); // prints "$5"
}