Resources
A #[resource] value owns an external resource. Its sibling impl defines
fn close(consume self) returning unit. Scope cleanup invokes it automatically;
explicit close consumes the value and prevents a second cleanup.
#[resource]
type Connection { id: i64; }
impl Connection {
fn close(consume self) { println(f"closed {self.id}"); }
}
fn main() {
let connection = Connection { id: 7 };
println("working");
}
Keep fallible completion separate: a finish, flush or commit operation returns
its own Result for the caller to handle. Cleanup is not a place to discard a
completion error. defer runs in LIFO order within its block; explicit process
exit does not run it. Resource moves and borrowed uses remain checked through
records, calls and supported actor boundaries.
Ordinary data such as JSON values needs no manual close or free. Use resources for external identity and lifetime, not for bookkeeping around ordinary data.
Use #[linear] for a value that must be consumed explicitly but has no
automatic cleanup operation. A live linear value must be moved into a
consuming function or method on every exit path. Choose #[resource] when
scope exit can safely call close; choose #[linear] when the caller must
make the terminal action explicit.