Int
Associated Functions for the Int type
Built-in associated functions for the Int type.
Arithmetic
div
fn div(self, divisor: Int) -> Int
Performs integer division (truncated toward zero).
Parameters
divisor— The divisor
Returns
The quotient of self / divisor, truncated toward zero.
Example
7.div(3) // 2
-7.div(3) // -2
7.div(-3) // -2
-7.div(-3) // 2
rem
fn rem(self, divisor: Int) -> Int
Performs the remainder operation (can return negative values).
Parameters
divisor— The divisor
Returns
The remainder of self / divisor. The result has the same sign as self.
Example
7.rem(3) // 1
-5.rem(3) // -2
-1.rem(5) // -1
7.rem(-3) // 1
Basic Math
abs
fn abs(self) -> Int
Returns the absolute value.
Returns
The non-negative value of self.
Example
-42.abs() // 42
42.abs() // 42
sign
fn sign(self) -> Int
Returns the sign of the integer.
Returns
-1ifselfis negative0ifselfis zero1ifselfis positive
Example
10.sign() // 1
0.sign() // 0
-42.sign() // -1
Comparison
min
fn min(self, other: Int) -> Int
Returns the smaller of two integers.
Parameters
other— The value to compare against
Returns
The minimum of self and other.
Example
10.min(20) // 10
-5.min(3) // -5
max
fn max(self, other: Int) -> Int
Returns the larger of two integers.
Parameters
other— The value to compare against
Returns
The maximum of self and other.
Example
10.max(20) // 20
-5.max(3) // 3
clamp
fn clamp(self, min_value: Int, max_value: Int) -> Int
Restricts the value to be within the specified range.
Parameters
min_value— The minimum allowed valuemax_value— The maximum allowed value
Returns
min_valueifself < min_valuemax_valueifself > max_valueselfotherwise
Example
5.clamp(0, 10) // 5
15.clamp(0, 10) // 10
-5.clamp(0, 10) // 0
Random
rnd
fn rnd(self) -> Int
Returns a deterministic pseudo-random integer based on the seed value.
Uses self as a seed to a noise function, producing the same output for the same
input. This is useful for procedural generation where repeatability is required.
Uses a high-quality, fast noise function designed for procedural generation. (e.g. squirrel noise function v5)
Returns
A pseudo-random integer in the full Int range (from Int::MIN to Int::MAX).
The same seed always produces the same result.
Example
42.rnd() // Always returns the same value for seed 42
100.rnd() // Always returns the same value for seed 100
Conversion
float
fn float(self) -> Float
Converts the integer to a [Float].
Returns
The [Float] representation of self.
Example
42.float() // 42.0
-5.float() // -5.0
string
fn string(self) -> String
Converts the integer to a [String].
Returns
The [String] representation of self.
Example
42.string() // "42"
-15.string() // "-15"
0.string() // "0"