Home

StdinLock

StdinLock is a locked, borrowing handle to the standard input provided by Rust’s standard library. It is produced by calling the lock method on a Stdin handle (for example, std::io::stdin().lock()) and represents exclusive access to standard input for the duration of the lock. The type is std::io::StdinLock<'a>, where 'a is the lifetime of the borrowed Stdin.

StdinLock implements the Read and BufRead traits, enabling both byte-wise and line-oriented input operations. This includes

The lock is released automatically when the StdinLock value goes out of scope (or is dropped), returning

Usage typically involves obtaining a lock once and reusing it for subsequent reads within the scope. For

let stdin = std::io::stdin();

let mut lock = stdin.lock();

let mut line = String::new();

lock.read_line(&mut line).unwrap();

StdinLock is a lightweight, ephemeral guard around the global standard input. It is designed for simplicity

methods
such
as
read,
read_line,
read_to_string,
and
the
lines()
iterator,
as
well
as
the
lower-level
BufRead
methods
like
fill_buf
and
consume.
Because
it
implements
BufRead,
it
provides
buffered
access
to
input
without
requiring
repeated
system
calls
for
each
read.
stdin
to
an
unlocked
state.
This
mechanism
prevents
data
races
when
multiple
parts
of
a
program
might
attempt
to
read
from
standard
input
concurrently
and
ensures
that
a
single,
consistent
stream
is
observed
during
the
lock’s
lifetime.
example:
and
safety
in
single-process
programs,
and
it
can
be
wrapped
or
used
directly
alongside
other
standard
I/O
utilities
in
Rust.
See
also:
Stdin,
Read,
BufRead,
StdinLock
and
other
lock
types
like
StdoutLock.