stdio
import std::io::stdio; · source
Three thin handles over file descriptors 0, 1, and 2, obtained from stdin(), stdout(), and stderr(). Each implements Read or Write and inherits every default on that trait. There is no buffering at this layer: stdout() translates to raw write(2) calls. Compose it explicitly with buf when you want it.
Types
Stdin
type struct Stdin {
is_tty(&this) -> boolean;
as_fd(&this) -> i32;
lock(&this) -> StdinLock;
line(mut &this) -> Result<Option<String>, IoError>;
prompt(mut &this, prompt_msg: Str) -> Result<Option<String>, IoError>;
}
import std::io::stdio;
mut input: Stdin = stdin();
match (input.line()) {
Result::Ok(Option::Some(text)) => { /* one line, newline stripped */ }
Result::Ok(Option::None) => { /* EOF */ }
Result::Err(e) => { /* IoError */ }
}
Every handle carries is_tty(), as_fd(), and lock(). Stdin adds two conveniences: line() reads one line into an owned String with the newline stripped, and prompt(msg) writes the prompt to stderr — so it doesn't pollute piped stdout — then reads a line.
Methods
is_tty(&this) -> boolean;
is_tty(&this) -> boolean;True if stdin is attached to a terminal. False when piped, redirected, or closed.
as_fd(&this) -> i32;
as_fd(&this) -> i32;Underlying file descriptor. Exposed for interop with code that still needs to pass fd 0 to a system call directly.
lock(&this) -> StdinLock;
lock(&this) -> StdinLock;Acquire the process-global stdin lock, blocking until it is free. The returned StdinLock reads through fd 0 and releases the lock on drop. See the module doc for the ownership rules.
line(mut &this) -> Result<Option<String>, IoError>;
line(mut &this) -> Result<Option<String>, IoError>;Read one line of input and return it as an owned String with the trailing \n (and a preceding \r, if any) stripped. None means EOF before any bytes were read.
prompt(mut &this, prompt_msg: Str) -> Result<Option<String>, IoError>;
prompt(mut &this, prompt_msg: Str) -> Result<Option<String>, IoError>;Write prompt_msg to stderr and read one line from stdin, trailing newline stripped. The prompt goes to stderr (not stdout) so piping the program's output doesn't capture the prompt text, the convention every well-behaved CLI follows.
Stdout and Stderr
type struct Stdout {
is_tty(&this) -> boolean;
as_fd(&this) -> i32;
lock(&this) -> StdoutLock;
}
type struct Stderr {
is_tty(&this) -> boolean;
as_fd(&this) -> i32;
lock(&this) -> StderrLock;
}
The two write handles, identical in shape. eprint and friends in fmt write through Stderr.
Stdout
Methods
is_tty(&this) -> boolean;
as_fd(&this) -> i32;
lock(&this) -> StdoutLock;
lock(&this) -> StdoutLock;Acquire the process-global stdout lock, blocking until it is free. The returned StdoutLock writes through fd 1 and releases the lock on drop. See the module doc for the ownership rules.
Stderr
Methods
is_tty(&this) -> boolean;
as_fd(&this) -> i32;
lock(&this) -> StderrLock;
lock(&this) -> StderrLock;Acquire the process-global stderr lock, blocking until it is free. The returned StderrLock writes through fd 2 and releases the lock on drop. See the module doc for the ownership rules.
The lock guards
type struct StdinLock {}
type struct StdoutLock {}
type struct StderrLock {}
lock() returns a guard that holds the stream's process-wide mutex until it drops, so a burst of writes from one thread is not interleaved with another's. The guards implement the same trait as the stream they lock, and release the mutex when dropped.
Trait implementations
Stdin
implement trait Read for struct Stdin
Stdout
implement trait Write for struct Stdout
implement trait FmtWrite for struct Stdout
std::fmt::write
Stderr
implement trait Write for struct Stderr
implement trait FmtWrite for struct Stderr
std::fmt::write
StdinLock
implement trait Read for struct StdinLock
implement trait Drop for struct StdinLock
StdoutLock
implement trait Write for struct StdoutLock
implement trait Drop for struct StdoutLock
StderrLock
implement trait Write for struct StderrLock
implement trait Drop for struct StderrLock
Free Functions
function stdin() -> Stdin;
function stdin() -> Stdin;Handle for process standard input.
function stdout() -> Stdout;
function stderr() -> Stderr;
function is_tty(fd: i32) -> boolean;
function is_tty(fd: i32) -> boolean;True if fd is a terminal. Used to choose line-buffered vs block-buffered output, to enable ANSI color, or to gate interactive prompts. libc::isatty returns nonzero for a terminal and 0 for everything else. Only zero means "not a TTY": msvcrt's _isatty answers 64 for a console, so a test against 1 reads every Windows terminal as redirected.
Constants
const STDIN_FD: i32 = 0;
const STDOUT_FD: i32 = 1;
const STDERR_FD: i32 = 2;
const LINE_FEED: u8 = 0x0A;
const NEWLINE_BYTES: u8* = "\n";
const NEWLINE: Str = Str::new("\n");type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct Stdin {}Handle to process standard input. Construction is free; all instances dispatch to file descriptor 0. Unbuffered: a line read that needs minimum syscalls should wrap this in a BufReader<Stdin> from io::buf.
Stdin::is_tty(&this) -> booleanTrue if stdin is attached to a terminal. False when piped, redirected, or closed.
&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
Stdin::as_fd(&this) -> i32Underlying file descriptor. Exposed for interop with code that still needs to pass fd 0 to a system call directly.
Stdin::lock(&this) -> StdinLockAcquire the process-global stdin lock, blocking until it is free. The returned StdinLock reads through fd 0 and releases the lock on drop. See the module doc for the ownership rules.
type struct StdinLock {}RAII guard over the stdin lock; reads through fd 0. Get one from stdin().lock(). Inherits the Read trait defaults (read_line, read_to_string, ...).
Stdin::line(mut &this) -> Result<Option<String>, IoError>Read one line of input and return it as an owned String with the trailing \n (and a preceding \r, if any) stripped. None means EOF before any bytes were read.
mut &this
mut &TypeAn exclusive, mutating borrow. As a receiver, mut &this lets the method modify the fields - and callers can see that from the signature alone. As a parameter type, mut &T lets the callee mutate through the reference while the caller keeps ownership.
type struct String<A = GlobalAlloc> {
buffer: RawBuffer<u8, A>;
length: u64;
}String: owned, heap-allocated, length-typed UTF-8.
std::collections::stringtype struct IoError {
kind: IoErrorKind;
os_code: i32;
}Stdin::prompt(mut &this, prompt_msg: Str) -> Result<Option<String>, IoError>Write prompt_msg to **stderr** and read one line from stdin, trailing newline stripped. The prompt goes to stderr (not stdout) so piping the program's output doesn't capture the prompt text, the convention every well-behaved CLI follows.
(parameter) prompt_msg: Strtype struct Str {
bytes: u8*;
length: u64;
}Str: a borrowed UTF-8 string slice.
std::collections::strimport a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
mut name: Type = value;A mutable binding, reassignable after declaration - mutability is opt-in, const is the default. At module level it declares mutable global state. Before & it marks an exclusive reference or receiver instead.
mut input: Stdintype struct Stdin {}Handle to process standard input. Construction is free; all instances dispatch to file descriptor 0. Unbuffered: a line read that needs minimum syscalls should wrap this in a BufReader<Stdin> from io::buf.
match (subject) {
Pattern(x) => { ... }
_ => { ... }
}The primary discriminator: branches on enum variants, integers, ranges, and destructuring patterns, and the compiler rejects a match that misses a case. There is no fallthrough. As an expression it evaluates to the chosen arm's value, so every arm must produce the same type.
mut input: StdinStdin::line(mut &this) -> Result<Option<String>, IoError>Read one line of input and return it as an owned String with the trailing \n (and a preceding \r, if any) stripped. None means EOF before any bytes were read.
Result::Ok(T)Option::Some(T)text: Option<String>pattern => { body }Separates a match arm's pattern from its body. Arms do not fall through.
Option::NoneResult::Err(E)e: IoErrortype struct Stdout {}Handle to process standard output. Unbuffered; wrap in LineWriter<Stdout> or BufWriter<Stdout> from io::buf for the common "coalesce many small writes" case.
Stdout::is_tty(&this) -> booleanStdout::as_fd(&this) -> i32Stdout::lock(&this) -> StdoutLockAcquire the process-global stdout lock, blocking until it is free. The returned StdoutLock writes through fd 1 and releases the lock on drop. See the module doc for the ownership rules.
type struct StdoutLock {}RAII guard over the stdout lock; writes through fd 1. Get one from stdout().lock(). Inherits the Write trait default write (generic over the payload).
type struct Stderr {}Handle to process standard error. Unbuffered by convention so diagnostics appear immediately; do **not** wrap in BufWriter unless you also guarantee a flush on every path that might panic or exit.
Stderr::is_tty(&this) -> booleanStderr::as_fd(&this) -> i32Stderr::lock(&this) -> StderrLockAcquire the process-global stderr lock, blocking until it is free. The returned StderrLock writes through fd 2 and releases the lock on drop. See the module doc for the ownership rules.
type struct StderrLock {}RAII guard over the stderr lock; writes through fd 2. Get one from stderr().lock().
type struct StdinLock {}RAII guard over the stdin lock; reads through fd 0. Get one from stdin().lock(). Inherits the Read trait defaults (read_line, read_to_string, ...).
type struct StdoutLock {}RAII guard over the stdout lock; writes through fd 1. Get one from stdout().lock(). Inherits the Write trait default write (generic over the payload).
type struct StderrLock {}RAII guard over the stderr lock; writes through fd 2. Get one from stderr().lock().
const NAME: Type = value;At module level, a true compile-time constant, conventionally SCREAMING_SNAKE_CASE. (mut at module level declares mutable global state instead.)
const STDIN_FD: i32 = 0;const STDOUT_FD: i32 = 1;const STDERR_FD: i32 = 2;const LINE_FEED: u8 = 0x0A;const NEWLINE_BYTES: u8* = "\n";const NEWLINE: Str = Str::new("\n");static Str::new(text: string) -> StrConstruct from a NUL-terminated C-style string literal. The caller certifies that text is valid UTF-8 and outlives this view. The NUL terminator is not included in the view.
function stdout() -> Stdoutfunction stderr() -> Stderrtype trait Read {
read(mut &this, buffer: u8*, length: u64) -> Result<u64, IoError>;
read_all(mut &this, buffer: u8*, length: u64) -> Result<u64, IoError>;
read_byte(mut &this) -> Result<Option<u8>, IoError>;
read_char(mut &this) -> Result<Option<u32>, IoError>;
read_until(mut &this, delimiter: u8, out: mut &Array<u8>) -> Result<u64, IoError>;
read_exact(mut &this, buffer: u8*, length: u64) -> Result<(), IoError>;
read_to_end(mut &this, out: mut &Array<u8>) -> Result<u64, IoError>;
read_to_string(mut &this, out: mut &String) -> Result<u64, IoError>;
read_line(mut &this, out: mut &String) -> Result<u64, IoError>;
}type trait Write {
write_some(mut &this, bytes: Slice<u8>) -> Result<u64, IoError>;
flush(mut &this) -> Result<(), IoError>;
write<T>(mut &this, data: T) -> Result<(), IoError>;
}Stdout::write<T>(mut &this, data: T) -> Result<(), IoError>via trait Write
Write data in full, retrying short writes and Interrupted errors. Generic over the payload, dispatched at compile time via static match: a raw byte Slice<u8>, a Str view, a NUL-terminated C string, or a single u8.
Stdin::is_tty(&this) -> booleanTrue if stdin is attached to a terminal. False when piped, redirected, or closed.
std::io::stdioStdin::as_fd(&this) -> i32Underlying file descriptor. Exposed for interop with code that still needs to pass fd 0 to a system call directly.
std::io::stdioStdin::lock(&this) -> StdinLockAcquire the process-global stdin lock, blocking until it is free. The returned StdinLock reads through fd 0 and releases the lock on drop. See the module doc for the ownership rules.
Stdin::prompt(mut &this, prompt_msg: Str) -> Result<Option<String>, IoError>Write prompt_msg to **stderr** and read one line from stdin, trailing newline stripped. The prompt goes to stderr (not stdout) so piping the program's output doesn't capture the prompt text, the convention every well-behaved CLI follows.
type struct Stderr {}Handle to process standard error. Unbuffered by convention so diagnostics appear immediately; do **not** wrap in BufWriter unless you also guarantee a flush on every path that might panic or exit.
implement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
implement trait Trait for Type { ... }Introduces a trait implementation: the block supplies, for one concrete type, the method bodies the trait requires.
language referenceimplement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language referencetype struct Stdout {}Handle to process standard output. Unbuffered; wrap in LineWriter<Stdout> or BufWriter<Stdout> from io::buf for the common "coalesce many small writes" case.
type trait FmtWrite {
write_str(mut &this, source: Str) -> Result<(), FmtError>;
write_char(mut &this, c: char) -> Result<(), FmtError>;
}function name<T>(param: Type) -> Ret { ... }Declares a function. Parameters are written name: Type and are never inferred; the return type follows -> and defaults to void. Declaration order does not matter: every signature is collected before any body is checked, so functions may call each other freely and recurse.
function stdin() -> StdinHandle for process standard input.
function stdout() -> Stdoutfunction stderr() -> Stderrfunction is_tty(fd: i32) -> booleanTrue if fd is a terminal. Used to choose line-buffered vs block-buffered output, to enable ANSI color, or to gate interactive prompts. libc::isatty returns nonzero for a terminal and 0 for everything else.
(parameter) fd: i32