Skip to content

Numbers and text

Integers are i8 i16 i32 i64 u8 u16 u32 u64; i64 is the default. There are no implicit conversions: x.to_u8(), n.to_i64() and so on convert and panic if the value does not fit (x.to_u8_checked() and the like give an Option instead), and every arithmetic operation panics on overflow. An integer literal takes the type it is used with (let b: u8 = 200, x + 1), and a literal that cannot fit is a compile error. Literals reach u64’s largest value (let max: u64 = 18446744073709551615), and -9223372036854775808 is i64’s smallest.

Floats are f64 (the default) and f32; a float literal takes the type it is used with, conversions are explicit (x.to_f32(), y.to_f64(), n.to_f32()), and f32 prints in the shortest form that reads back as the same f32.

Str holds UTF-8 bytes. len(), slice() and byte() work in bytes. Three views iterate it: bytes() gives the code units as Vec<u8>, codepoints() gives Vec<u32> (a malformed byte reads as U+FFFD, as in Go), and graphemes() gives the user-perceived characters as Vec<Str>, following Unicode 17’s extended grapheme cluster rules (UAX #29, including emoji sequences, flags and Indic conjuncts). The tables are generated by tools/gen_unicode.pl and linked in only by programs that use them.

String literals:

  • "..." with the escapes \n \t \r \0 \\ \" \', \xHH (one byte) and \u{1F600} (a Unicode character, written as UTF-8).
  • """ on its own line starts a multi-line string, which ends at a """ on a line of its own. The closing line’s indentation is removed from every line, so the text can be indented with the code around it; the line break before the closing """ is not part of the text, and a \ at the end of a line joins it to the next. f""" makes a multi-line f-string.
  • r"..." and r"""...""" are raw: a \ is just a backslash, for regular expressions, Windows paths and test data.
let name = "Brass"
let page = f"""
<h1>{name}</h1>
<p>"Quotes" need no escaping here, {{braces}} are doubled.</p>
"""
print(page)
print(r"C:\new\temp \d+")
print("caf\u{e9} \x41")
<h1>Brass</h1>
<p>"Quotes" need no escaping here, {braces} are doubled.</p>
C:\new\temp \d+
café A