Primitive type auto-imported

string

type string
Source: compiler/src/builtin_docs/types.hy

A UTF-8 text string.

Literals are written in double quotes, with escapes \\, \", \n, \r, \t, \0, \e, \xHH and \u{HEX}. + and += concatenate two strings (adding a non-string is a type error). == compares contents, and <, <=, >, >= order strings by their UTF-8 bytes (string is Ord, so collections::sort sorts a Vec<string>). s.len() counts UTF-8 bytes, not characters.

A string is not cast with as: use string::to_bytes for its bytes and string::format to build strings from other values. A one-byte string literal coerces to byte where a byte is expected.

Examples#

use tests::assert;
let s = "héllo";
assert!(s.len() == 6);
let greeting = "hi" + ", " + "there";
greeting += "!";
assert!(greeting == "hi, there!");
assert!("apple" < "banana" && "B" < "a");
assert!(show(s) == "héllo");

See also#

↑ ↓ navigate ↵ open