t27.aiРусский

Whole numbers and their widths

You will learn

The integer types, their ranges, and the error a value too big for its type gets.

A u in the type name means unsigned, an i means signed, and the digits are the number of bits. u8 holds 0 to 255, u16 holds 0 to 65535, i32 holds negative numbers too. The compiler checks a constant against its type: give a u8 the value 300 and it stops with an error that says no u8 can hold 3 digits. The browser's checker is lenient elsewhere, so do not read its silence as proof.

Try it

Set BYTE_MAX to 300 in the player and read the error the compiler prints.

Open the interactive lesson →

t27 basics 5: integer widths and their ranges
t27 basics 5: integer widths and their ranges ↗

u8, u16, u32 and i32 with their bit counts and ranges, and the error t27c gives a value too big for its type. Lesson 5 of the t27 basics course.

specs/basics/05_whole_numbers.t27

// SPDX-License-Identifier: Apache-2.0
; t27 basics, lesson 5: Whole numbers and their widths.
; u8, u16, u32 and i32: how many bits a number has decides which values it can hold.

module basics_05_whole_numbers;

pub const BYTE_MAX : u8 = 255;
pub const WORD_MAX : u16 = 65535;
pub const SMALL_NEG : i32 = -5;

test wider_types_hold_more {
    assert WORD_MAX > BYTE_MAX;
    assert BYTE_MAX == 255;
    assert SMALL_NEG < 0;
}

Open the lesson's spec in the player ↗

All lessons