Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Integers

ItemSummary
absReturns the absolute value
minReturns the smaller of the number and other
maxReturns the larger of the number and other
clampReturns the number moved into the range from low to high, inclusive
to_strReturns the number written out in base 10, with a leading - if it’s negative
to_floatReturns the number as a float
randomReturns a random int that is at least 0 and less than len

Methods

abs

fn abs(self) -> int

Returns the absolute value. The smallest int, -9223372036854775808, has no positive counterpart, and its absolute value is a runtime error.

let a = (-7).abs(); // 7

min

fn min(self, other: int) -> int

Returns the smaller of the number and other.

let a = 3.min(8); // 3

max

fn max(self, other: int) -> int

Returns the larger of the number and other.

let a = 3.max(8);        // 8
let hp = (5 - 9).max(0); // 0

clamp

fn clamp(self, low: int, high: int) -> int

Returns the number moved into the range from low to high, inclusive. A number already in the range comes back unchanged.

A low greater than high is a runtime error.

let a = 15.clamp(0, 10);   // 10
let b = (-3).clamp(0, 10); // 0
let c = 4.clamp(0, 10);    // 4

to_str

fn to_str(self) -> str

Returns the number written out in base 10, with a leading - if it’s negative. An f-string does the same inside a larger string.

let a = (-42).to_str(); // "-42"
let b = f"{7} lives";   // "7 lives"

to_float

fn to_float(self) -> float

Returns the number as a float. Arithmetic that mixes int and float converts on its own, but passing an int where a float is expected doesn’t, and needs this first:

fn half(x: float) -> float {
    x / 2.0
}

let n = 3;
let a = half(n.to_float());  // 1.5
let b = n.to_float().sqrt(); // 1.7320508075688772

Associated Functions

random

fn random(len: int) -> int

Returns a random int that is at least 0 and less than len. That makes int::random(xs.len()) a random index into xs.

A len that isn’t greater than 0 is a runtime error.

let roll = int::random(6) + 1; // 1 to 6