Types
PanicValue
Section titled “PanicValue”A value captured from a recover block after a panic.
fn will_panic() -> int { panic("boom")}
let result = recover { will_panic() }Result<int, PanicValue>
match result { Ok(value) => fmt.Println(value), Err(pv) => fmt.Println("recovered from a panic"),PanicValue}PanicValue.message()
Section titled “PanicValue.message()”Returns the message string in the PanicValue.
impl PanicValue { fn message(self) -> string}let digits = [1, 2, 3]let result = recover { digits[10] }Result<int, PanicValue>
match result { Ok(value) => fmt.Println(value), Err(pv) => fmt.Println(pv.message()),"index out of range [10] with length 3"}PanicValue.as_error()
Section titled “PanicValue.as_error()”Returns the PanicValue as an error, if it implements the error interface.
impl PanicValue { fn as_error(self) -> Option<error>}let digits = [1, 2, 3]let result = recover { digits[10] }Result<int, PanicValue>
match result { Ok(value) => fmt.Println(value), Err(pv) => match pv.as_error() { Some(err) => fmt.Println(err.Error()),"index out of range [10] with length 3" None => fmt.Println(pv.message()), },}PanicValue.stack_trace()
Section titled “PanicValue.stack_trace()”Returns the stack trace at the point of the panic.
impl PanicValue { fn stack_trace(self) -> string}let digits = [1, 2, 3]let result = recover { digits[10] }Result<int, PanicValue>
match result { Ok(value) => fmt.Println(value), Err(pv) => fmt.Println(pv.stack_trace()),"goroutine 1 [running]:
runtime/debug.Stack()
prelude.RecoverBlock[...].func1.1()
..."}Indicates a function never returns, e.g. functions that panic or loop forever.
fn fail(msg: string) -> Never { panic(msg)}
fn serve() -> Never { loop { handle(accept()) }}The type returned when there is no meaningful value.
fn save(path: string) -> Result<(), error> { os.WriteFile(path, "data".bytes(), 0o644)}
match save("out.txt") { Ok(()) => fmt.Println("saved"),Unit Err(err) => fmt.Println(err.Error()),}Unknown
Section titled “Unknown”Equivalent to Go’s any.
let mut fields: Map<string, Unknown> = Map.new()let _ = json.Unmarshal("{\"name\": \"alice\"}".bytes(), &fields)
match fields.get("name") { Some(value) => { let name = assert_type<string>(value)Some("alice") fmt.Println(name) }, None => fmt.Println("missing"),}VarArgs
Section titled “VarArgs”Equivalent to Go’s ...T.
fn sum(numbers: VarArgs<int>) -> int { let mut total = 0 for n in numbers { total += n } total}
let total = sum(1, 2, 3)error interface
Section titled “error interface”The interface every error type satisfies, by having an Error() method.
interface error { fn Error() -> string}match os.ReadFile("missing.txt") { Ok(bytes) => fmt.Println(bytes.length()), Err(err) => fmt.Println(err.Error()),"open missing.txt: no such file or directory"}