expression
Transfer ownership of a Unix file descriptor
OwnedFd represents responsibility for closing a Unix-like descriptor on drop. Moving a File into OwnedFd and back transfers that responsibility without manufacturing a descriptor number or manually closing it. The fixture creates a new scratch file and uses safe conversions. File descriptor numbers can be reused by the OS after closure. Expected stdout (not executed): owned Common mistake: Constructing multiple independent owners for the same raw descriptor. Run only in the isolated fixture runner, not a public code playground.
Formal Syntax & Structure
rust syntaxrust
let fd: OwnedFd = file.into();Working Example
Example Snippetrust
use std::{fs::{File, OpenOptions}, io::{self, Write}, os::fd::OwnedFd};fn main() -> io::Result<()> { let file = OpenOptions::new().write(true).create_new(true).open("owned.txt")?; let fd: OwnedFd = file.into(); let mut file: File = fd.into(); file.write_all(b"owned")?; drop(file); println!("{}", std::fs::read_to_string("owned.txt")?); Ok(())}