Skip to content

Runes

Runes represent Unicode code points in Harneet. A rune is a 32-bit integer value (alias of int32) that encodes a single Unicode scalar value.

Overview

  • Runes are 32-bit Unicode code points (alias of int32)
  • Literal syntax uses single quotes: 'A', '😀', '\n'
  • Supports escape sequences and Unicode escapes
  • Arithmetic and comparisons behave like integers
  • Use strings.FromCodePoint(int) to turn a code point into a 1-character string

Rune Literals

Single-quoted rune literals:

package main

var a rune = 'a'
var Z rune = 'Z'
var newline rune = '\n'
var tab rune = '\t'
var backslash rune = '\\'
var quote rune = '\''

// Hex escape
var hexA rune = '\x41'   // 'A'

// Unicode escapes
var uA rune = '\u0041'       // 'A'
var face rune = '\U0001F600'  // 😀

Supported Escapes

  • \n, \t, \r, \\, \', \0
  • \xNN 2-digit hex (0..0xFF)
  • \uNNNN 4-digit hex
  • \UNNNNNNNN 8-digit hex

Arithmetic and Comparison

Runes are integers. You can use them in arithmetic and comparisons.

package main
import fmt

function main() {
    var c rune = 'a'
    var next = c + 1     // 98
    fmt.Printf("next: %d\n", next)

    if 'a' < 'z' {
        fmt.Println("'a' is less than 'z'")
    }
}

Printing

fmt.Printf treats runes as integers. Use %d to print the numeric code point.

fmt.Printf("'A' code point: %d\n", 'A')  // 65

To convert a code point to a string, use strings.FromCodePoint:

1
2
3
4
import strings

var s, err = strings.FromCodePoint('A')
// s == "A"

Conversions

  • Rune ↔ Int: implicit in most operations (rune is an integer type)
  • Rune → String: use strings.FromCodePoint(int)
  • String → Rune: use cast.ToRune(string) for a single-character string
1
2
3
4
5
6
7
8
import strings, cast, fmt

// int (code point) to string
a, _ = strings.FromCodePoint(65)   // "A"

// single-character string to code point
cp, _ = cast.ToRune("A")          // 65
fmt.Printf("%d\n", cp)

Arrays and Maps of Runes

package main
import fmt

function main() {
    // Array (untyped) of runes
    var hello = ['H', 'e', 'l', 'l', 'o']
    fmt.Printf("hello: %v\n", hello)   // [72 101 108 108 111]

    // Maps with rune keys
    var nato = { 'A': "Alpha", 'B': "Bravo" }
    fmt.Println(nato['A'])  // "Alpha"
}

Notes

  • Zero value for rune is 0.
  • Valid range is 0..0x10FFFF (Unicode scalar values). Out-of-range values are rejected.