t27.aiРусский

Constants

You will learn

The six pieces of a constant declaration and what each one does.

pub const WIDTH : u8 = 8; reads left to right: pub makes the name visible to other modules, const says the value is fixed, WIDTH is the name, u8 is the type, 8 is the value, and the semicolon ends it. Constants are what this site's test runner can check: the lesson spec declares WIDTH, HEIGHT and AREA and tests that 8 times 4 is 32.

Try it

Change AREA in the lesson spec to 33 and watch which test fails.

Open the interactive lesson →

t27 basics 4: a constant, piece by piece
t27 basics 4: a constant, piece by piece ↗

The six pieces of a t27 constant declaration in one table. Lesson 4 of the t27 basics course.

specs/basics/04_constants.t27

// SPDX-License-Identifier: Apache-2.0
; t27 basics, lesson 4: Constants.
; A constant gives a name, a type and a value, and the name never changes its value.

module basics_04_constants;

pub const WIDTH : u8 = 8;
pub const HEIGHT : u8 = 4;
pub const AREA : u8 = 32;

test area_is_width_times_height {
    assert WIDTH * HEIGHT == AREA;
}

Open the lesson's spec in the player ↗

All lessons