typestar

Arrays and slices in Odin

A fixed week of temperatures and a slice borrowing three days of it.

package main

import "core:fmt"

main :: proc() {
    // A fixed array's length is part of the type.
    week := [7]int{61, 68, 74, 79, 72, 65, 58}

    // Slicing borrows a view: pointer plus length, same memory.
    midweek := week[2:5]
    fmt.println("midweek:", midweek)
    fmt.println("len", len(midweek), "first", midweek[0])

    midweek[0] += 1
    fmt.println("warmed wednesday:", week[2])
}

How it works

  1. [7]int{...} fixes the length into the type.
  2. week[2:5] is a slice: pointer plus length onto the same memory.
  3. Writing through the slice warms Wednesday in the array underneath.

Keywords and builtins used here

The run, in numbers

Lines
16
Characters to type
370
Tokens
95
Three-star pace
70 tpm

At the three-star pace of 70 tokens a minute, this run takes about 81 seconds.

Type this snippet

Step 1 of 3 in Collections, step 10 of 27 in Language basics.

← Previous Next →