errno is a process-global error indicator used by C and POSIX system calls. A call such as close() may return -1 and set errno to a code describing the failure. section 3.4.1
Checking close()
If close() is interrupted by a caught signal, it returns -1 with errno set to EINTR. An input/output failure may set errno to EIO. These constants have operating-system-specific values. section 3.4.1
A global error-code pattern typically works like this:
- Call an operation such as read() or close(). section 3.4.1
- Check the global error value immediately after the call. section 3.4.1
- Handle a nonzero code, often by reporting or propagating the failure. section 3.4.1
Listing 3.6 Rust-like code that checks error codes from a global variable
static mut ERROR: i32 = 0; #1
// ...
fn main() {
let mut f = File::new("something.txt");
read(f, buffer);
unsafe { #2
if ERROR != 0 { #3
panic!("An error has occurred while reading the file ")
}
}
close(f);
unsafe { #2
if ERROR != 0 { #3
panic!("An error has occurred while closing the file ")
}
}
}
The compact listing shows the unsafe Rust-like form of checking a mutable global error variable after file operations. section 3.4.1


