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

Prelude

ItemSummary
printWrites value to standard output, followed by a newline
panicStops the script with a runtime error
todoStops the script with a runtime error that marks unfinished code
dbgPrints value to standard output in its debug form, then returns it

Functions

print

fn print(value)

Writes value to standard output, followed by a newline. A string prints as its plain text, and any other value prints the same way it would inside an f-string.

print("hello");           // hello
print([1, 2, 3]);         // [1, 2, 3]
print(f"{1 + 2} apples"); // 3 apples

panic

fn panic(msg: T?) -> !

Stops the script with a runtime error. The error reads panic: followed by msg, or explicit panic when there’s no message.

panic never returns, and its never type lets it stand in for a value of any type:

let config = ~{ port = 8080 };
let port = config["port"] ?? panic("no port configured");

For a failure the caller should be able to recover from, return a result and raise instead.

todo

fn todo(msg: T?) -> !

Stops the script with a runtime error that marks unfinished code. The error reads todo: followed by msg, or just todo when there’s no message.

Like panic, it never returns, and it can stand in for a body you haven’t written yet:

fn load_save(path: str) -> ~{int} {
    todo("read the save format")
}

dbg

fn dbg(value: T) -> T

Prints value to standard output in its debug form, then returns it. The line starts with dbg value:, and strings keep their quotes.

Since it returns its argument, dbg can wrap any expression without changing the result:

let total = dbg(2 + 3) * 10; // prints `dbg value: 5`, and total is 50
let name = dbg("ada");       // prints `dbg value: "ada"`