Function std::io::stdin 1.0.0[−][src]
Expand description
为当前进程的标准输入创建一个新的句柄。
返回的每个句柄都是对共享缓冲区的引用,该缓冲区的访问通过互斥锁进行同步。
如果需要对锁定进行更明确的控制,请参见 Stdin::lock
方法。
Note: Windows 可移植性注意事项
在控制台中操作时,此流的 Windows 实现不支持非 UTF-8 字节序列。
尝试读取无效的 UTF-8 字节将返回错误。
Examples
使用隐式同步:
use std::io;
fn main() -> io::Result<()> {
let mut buffer = String::new();
io::stdin().read_line(&mut buffer)?;
Ok(())
}
Run使用显式同步:
use std::io::{self, BufRead};
fn main() -> io::Result<()> {
let mut buffer = String::new();
let stdin = io::stdin();
let mut handle = stdin.lock();
handle.read_line(&mut buffer)?;
Ok(())
}
Run