panic_unwind
import std::core::panic_unwind; · source
Types
PanicInfo
type struct PanicInfo {
msg: string;
file: string;
line: u32;
}
What a caught panic carries: the message, and the file and line of the panic call that raised it. It is the Err payload of catch_unwind.
Properties
| Property | Type |
|---|---|
msg | string |
file | string |
line | u32 |
Free Functions
function catch_unwind<T>(f: () -> T) -> Result<T, PanicInfo>;
function catch_unwind<T>(f: () -> T) -> Result<T, PanicInfo>;Run f, catching a panic that unwinds out of it. Returns Ok(v) with the body's result on a normal return, or Err(info) if f (or anything it transitively calls) panicked. See the module doc for the requirements and the abort-strategy caveat.
A panic that unwinds out of f becomes Err(PanicInfo) instead of reaching the process root:
import std::core::panic_unwind;
match (catch_unwind(risky)) {
Result::Ok(v) => { /* normal return */ }
Result::Err(info) => { /* info.msg, info.file, info.line */ }
}
This is what makes destructors run on a panic: by the two-phase unwind contract, a catch is the phase-1 handler that lets every intermediate frame's cleanup pad run its drops in phase 2. Without a catch, an uncaught panic finds no handler and skips phase 2 entirely.
It requires --panic=unwind (or panic = "unwind" in cryoconfig). Under the default abort strategy a panic terminates the process, so there is nothing to catch and calling catch_unwind is a compile error. f is a plain function pointer rather than a capturing closure — bind captured state into a named function, the same as thread::spawn.
The executor uses this at its poll boundary, which is how one task's panic becomes a JoinError::Panicked for its joiner rather than a crash for its siblings.
type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct PanicInfo {
msg: string;
file: string;
line: u32;
}The payload a caught panic reports: the panic message and the source location it was raised from. Mirrors the fields the runtime funnel (runtime/panic/unwind) stashes when it raises.
PanicInfo.msg: stringprimitive stringNUL-terminated raw string (u8*), matching the C ABI. Length-typed text is Str / String.
PanicInfo.file: stringPanicInfo.line: u32import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
match (subject) {
Pattern(x) => { ... }
_ => { ... }
}The primary discriminator: branches on enum variants, integers, ranges, and destructuring patterns, and the compiler rejects a match that misses a case. There is no fallthrough. As an expression it evaluates to the chosen arm's value, so every arm must produce the same type.
function catch_unwind<T>(f: () -> T) -> Result<T, PanicInfo>Run f, catching a panic that unwinds out of it. Returns Ok(v) with the body's result on a normal return, or Err(info) if f (or anything it transitively calls) panicked. See the module doc for the requirements and the abort-strategy caveat.
Result::Ok(T)v: Tpattern => { body }Separates a match arm's pattern from its body. Arms do not fall through.
Result::Err(E)info: PanicInfofunction panic(message: string, loc: intrinsics::Location) -> neverPanic with message, reporting the place loc: intrinsics::location() written at the call, or in a ![caller_location] function its caller's. The helpers below do that automatically.
PanicInfo.msg: stringPanicInfo.file: stringPanicInfo.line: u32function name<T>(param: Type) -> Ret { ... }Declares a function. Parameters are written name: Type and are never inferred; the return type follows -> and defaults to void. Declaration order does not matter: every signature is collected before any body is checked, so functions may call each other freely and recurse.
function catch_unwind<T>(f: () -> T) -> Result<T, PanicInfo>Run f, catching a panic that unwinds out of it. Returns Ok(v) with the body's result on a normal return, or Err(info) if f (or anything it transitively calls) panicked. See the module doc for the requirements and the abort-strategy caveat.
type parameter T(parameter) f: () -> Ttype struct PanicInfo {
msg: string;
file: string;
line: u32;
}The payload a caught panic reports: the panic message and the source location it was raised from. Mirrors the fields the runtime funnel (runtime/panic/unwind) stashes when it raises.
type enum JoinError {
Cancelled;
Panicked(PanicInfo);
}Why a join did not yield a value.
JoinError::Panicked(PanicInfo)