Module builtins

Built-in functions available in every Hew program.

These functions are always in scope — no import is needed. They cover I/O, assertions, math, string operations, and program control flow.

Contents

Functions

Function println

fn println(value: dyn Display)

Print a value followed by a newline.

Accepts any type that implements Display.

Examples

println(42);
println("hello");
println(true);

Function print

fn print(value: dyn Display)

Print a value without a trailing newline.

Examples

print("Enter name: ");

Function assert

fn assert(condition: bool)

Assert that a condition is true. Panics if condition is false.

Examples

assert(x > 0);

Function assert_eq

fn assert_eq(left: dyn Display, right: dyn Display)

Assert that two values are equal. Panics with a diff if they differ.

Both arguments must be the same type.

Examples

assert_eq(add(1, 2), 3);

Function assert_ne

fn assert_ne(left: dyn Display, right: dyn Display)

Assert that two values are not equal. Panics if they are the same.

Both arguments must be the same type.

Examples

assert_ne(a, b);

Function abs

fn abs(x: i32) -> i32

Returns the absolute value of an integer.

Examples

let x = abs(-5);  // 5

Function sqrt

fn sqrt(x: f64) -> f64

Returns the square root of a floating-point number.

Examples

let r = sqrt(9.0);  // 3.0

Function min

fn min(a: i32, b: i32) -> i32

Returns the smaller of two integers.

Function max

fn max(a: i32, b: i32) -> i32

Returns the larger of two integers.

Function to_float

fn to_float(x: i32) -> f64

Convert an integer to a floating-point number.

Examples

let f = to_float(42);  // 42.0

Function string_concat

fn string_concat(a: string, b: string) -> string

Concatenate two strings, returning a new string.

Examples

let full = string_concat("hello ", "world");

Function string_length

fn string_length(s: string) -> i32

Returns the byte length of a string.

Function to_string

fn to_string(value: dyn Display) -> string

Convert any value to its string representation.

Examples

let s = to_string(42);  // "42"

Function len

fn len(value: dyn Display) -> i32

Returns the length of a collection or string.

Function string_char_at

fn string_char_at(s: string, index: i32) -> char

Returns the character (as a char) at the given byte index.

Function string_equals

fn string_equals(a: string, b: string) -> bool

Test whether two strings are equal.

Function string_from_int

fn string_from_int(n: i32) -> string

Convert an integer to a string.

Examples

let s = string_from_int(42);  // "42"

Function string_contains

fn string_contains(haystack: string, needle: string) -> bool

Test whether haystack contains needle.

Function string_starts_with

fn string_starts_with(s: string, prefix: string) -> bool

Test whether a string starts with the given prefix.

Function substring

fn substring(s: string, start: i32, end: i32) -> string

Extract a substring by byte indices [start, end).

Examples

let s = substring("hello", 0, 3);  // "hel"

Function string_trim

fn string_trim(s: string) -> string

Strip leading and trailing whitespace.

Function string_to_int

fn string_to_int(s: string) -> i32

Parse a string as an integer. Returns 0 on failure.

Function string_find

fn string_find(haystack: string, needle: string) -> i32

Find the first occurrence of needle in haystack.

Returns the byte offset, or -1 if not found.

Function read_file

fn read_file(path: string) -> string

Read the entire contents of a file as a string.

Function write_file

fn write_file(path: string, content: string)

Write a string to a file, creating or overwriting it.

Function sleep_ms

fn sleep_ms(ms: i32)

Pause execution for the given number of milliseconds.

Function sleep

fn sleep(seconds: i32)

Pause execution for the given number of seconds.

Function stop

fn stop(target: dyn Display)

Stop an actor gracefully.

Function exit

fn exit(code: i32)

Terminate the program with the given exit code.

This function never returns.

Function panic

fn panic(message: string)

Terminate the program with an error message.

This function never returns.

Examples

panic("something went wrong");