Runes
Overview
Many languages have a type like char
which represents a character, usually ASCII,
since the size of char
is defined as 1 byte.
V does not have a char
type as such (the u8
type can be used instead),
instead V has a rune
type.
A rune
represents a single Unicode character and is an alias for u32
.
To denote them, use `
(backticks):
A rune
can be converted to a UTF-8 string by using the .str()
method.
A rune
can be converted to UTF-8 bytes by using the .bytes()
method.
Hex, Unicode, and Octal escape sequences also work in a rune
literal:
Note that rune
literals use the same escape syntax as strings,
but they can only hold one Unicode character.
Therefore, if your code does not specify a single Unicode character,
you will receive an error at compile time.
Also remember that strings are indexed as bytes, not runes, so beware:
A string can be converted to runes by the .runes()
method.