Files and processes are owners too
An open file, a child process and its pipes, and a handle from a C library carry a cleanup obligation in the same way a Buffer does. The binding that acquires one owns it, passing it by value hands the obligation on, and the release happens once.
Check reports a block that is left without releasing, and it reports a release through a copy that does not own the resource. defer on the line after the acquisition always satisfies both.
fn main() {
let notes = std.fs.try_open("notes.txt", "w").unwrap()
defer notes.close()
notes.write("moon\n")
println("saved")
}
Examples use the moon toolchain version shown beside each result.
Run locally
moon run main.mn
Ctrl/⌘+Enter checks the current lesson.