How to query file descriptors for stdin, stdout, stderr?

#1

Does the Rust standard library offer functions/macros for reliably obtaining file descriptors to the stdin, stdout, and stderr streams? Ordinarily, I would just use printf!() and so on, but in my case, I’m working with environments where stdout may not necessarily be 1, for example.

0 Likes

#2

https://doc.rust-lang.org/std/io/fn.stdin.html
https://doc.rust-lang.org/std/io/fn.stdout.html
https://doc.rust-lang.org/std/io/fn.stderr.html

You can then use the RawFD or RawHandle traits to get the appropriate handler

0 Likes

#3

I’m not sure I understand. By definition, stdin, stdout, and stderr are file descriptors 0, 1, and 2, respectively. std::io::stdin(), for example, always returns an object that uses file descriptor 0.

At a guess, is it that you want to be able to read and write from a console, regardless of if the standard IO FDs have been remapped?

0 Likes