expression

Duplicate a descriptor without assuming an independent cursor

On Linux, descriptor duplication shares the underlying open file description, including its seek offset. The two owners close their own descriptors independently. If independent cursors are required, open the file separately. This fixture reads adjacent bytes through two File values from duplicated descriptors. Expected stdout (not executed): A B Common mistake: Assuming a duplicated descriptor gives each file its own seek position. Run only in the isolated fixture runner, not a public code playground.

Formal Syntax & Structure

rust syntaxrust
OwnedFd::try_clone() -> io::Result<OwnedFd>

Working Example

Example Snippetrust
use std::{fs::{self, File}, io::{self, Read}, os::fd::OwnedFd};fn main() -> io::Result<()> {    fs::write("letters.txt", b"ABC")?;    let fd: OwnedFd = File::open("letters.txt")?.into();    let copy = fd.try_clone()?;    let mut a: File = fd.into(); let mut b: File = copy.into();    let mut x = [0]; let mut y = [0];    a.read_exact(&mut x)?; b.read_exact(&mut y)?;    println!("{} {}", char::from(x[0]), char::from(y[0])); Ok(())}
Rust Reference