Trait StdioExt
pub trait StdioExt
Required Methods
fn set_handle<T: Into<OwnedHandle>>(&mut self, handle: Option<T>) -> Result<()>Sets the stdio console handle to
handle, orNULLif it isNone. The old handle, if any, will not be closed, i.e. it is leaked because console handles are shared global resources.Rust std::io write buffers (if any) are flushed, but other runtimes (e.g. C stdio) or libraries that acquire a clone of the file handle will not be aware of this change.
#![feature(stdio_swap)] use std::io::{self, Read, Write}; use std::os::windows::io::StdioExt; fn main() -> io::Result<()> { let (reader, mut writer) = io::pipe()?; let mut stdin = io::stdin(); stdin.set_handle(Some(reader))?; writer.write_all(b"Hello, world!")?; let mut buffer = vec![0; 13]; assert_eq!(stdin.read(&mut buffer)?, 13); assert_eq!(&buffer, b"Hello, world!"); Ok(()) }fn replace_handle<T: Into<OwnedHandle>>(&mut self, replace_with: T) -> Result<Option<BorrowedHandle<'static>>>Sets the stdio console handle to
replace_with. The previous handle is returned, orNoneif it wasNULL.The returned handle is a
BorrowedHandle<'static>because console handles are shared global resources and may have been obtained by other functions or threads. Only if you have ensured that no other part of the program has borrowed this handle you can convert it into anOwnedHandleand drop that to close it.Like
set_handle(), Rust std::io write buffers (if any) are flushed.fn take_handle(&mut self) -> Result<Option<BorrowedHandle<'static>>>Sets the stdio console handle to
NULLand returns the old oneSee
set_handle()for additional details.