Runtime
Panics
A panic is a hard, unrecoverable runtime error. It prints a message and exits the process with a non-zero status. Panics cannot be caught.
Panics are triggered by:
.yolo()onNoneor aResult::Err- Out-of-bounds array access
- Integer division by zero
assert(false)orassert_msg(false, msg)
Built-in Functions
These are available in every module without any import declaration (provided by std::core auto-import):
| Name | Signature | Description |
|---|---|---|
print | <T>(v: T) | Print to stdout, no newline |
println | <T>(v: T) | Print to stdout with newline |
string_len | (s: String) -> i64 | Number of characters in a string |
string_concat | (a: String, b: String) -> String | Concatenate two strings |
clock | () -> i64 | Unix timestamp in milliseconds |
assert | (cond: boolean) | Panic with "assertion failed" if cond is false |
assert_msg | (cond: boolean, msg: String) | Panic with msg if cond is false |
dbg | <T>(v: T) -> T | Print [dbg] <value> to stderr and return the value unchanged |
Built-in Aspects
The following aspects are pre-implemented for built-in types:
Display
aspect Display {
fun to_string(&self) -> String;
}
i64, f64, boolean, String, and Char implement Display. .to_string() returns the canonical string representation. print and println accept any Display type.
Iterable<T>
aspect Iterable<T> {
fun next(&mut self) -> Perhaps<T>;
}
T[] (array) and Range (from .. / ..=) implement Iterable<T>. User-defined types may implement it to be usable in for-in.
From<S>
aspect From<S> {
fun from(value: S) -> Self;
}
i64 implements From<f64> (truncating cast) and f64 implements From<i64>. The as operator desugars to T::from(value). User-defined types may implement From<S> to enable as casts and ? error coercion.
String Methods
| Method | Signature | Description |
|---|---|---|
.len() | () -> i64 | Number of characters in the string |
.to_string() | () -> String | Returns the string itself |
Array Methods
T[] and [T; N] both expose:
| Method | Signature | Description |
|---|---|---|
.len() | () -> i64 | Number of elements |
Char Methods
Since v0.8.0.
| Method / Function | Signature | Description |
|---|---|---|
.to_u32() | () -> u32 | Unicode scalar value as a u32 |
Char::from_u32(n) | (u32) -> Perhaps<Char> | Construct from a code point; None if invalid |
.to_string() | () -> String | Single-character string |