How can I read from a specific raw file descriptor in Rust?

前端 未结 3 1514
小鲜肉
小鲜肉 2020-12-10 15:00

Editor\'s note: This question is for a version of Rust prior to 1.0. Some answers have been updated to cover Rust 1.0 or up, but not all.

<
3条回答
  •  有刺的猬
    2020-12-10 15:50

    Editor's note: This answer is for a version of Rust prior to 1.0 and does not compile in Rust 1.0.

    I'm just exploring rust myself, but after searching a bit, came up with the following use of the native crate: (using file descriptor "1" for the example -- 1 is stdout)

    extern crate native;
    use native::io::file::FileDesc as FileDesc;
    
    fn main() 
    {
      let mut f = FileDesc::new(1, true);
      let buf = "Hello, world!\n".as_bytes();
    
      f.inner_write(buf);
    }
    

    (Tested with rustc 0.13.0-nightly. Note that the inner_write method may have been renamed to write in more recent versions of libstd)

    Update

    The nativecrate was recently removed from the standard library and FileDesc, while still exists as std::sys::fs::FileDesc is no longer public...

提交回复
热议问题