nix::unistd

Function close

source
pub fn close(fd: RawFd) -> Result<()>
Expand description

Close a raw file descriptor

Be aware that many Rust types implicitly close-on-drop, including std::fs::File. Explicitly closing them with this method too can result in a double-close condition, which can cause confusing EBADF errors in seemingly unrelated code. Caveat programmer. See also close(2).

ยงExamples

use std::os::unix::io::AsRawFd;
use nix::unistd::close;

let f = tempfile::tempfile().unwrap();
close(f.as_raw_fd()).unwrap();   // Bad!  f will also close on drop!
use std::os::unix::io::IntoRawFd;
use nix::unistd::close;

let f = tempfile::tempfile().unwrap();
close(f.into_raw_fd()).unwrap(); // Good.  into_raw_fd consumes f