<C-c>
が入力されるまで受け付ける
fn main() {
// 標準入力へのハンドルを生成 (各ハンドルはバッファーを共有される)
let stdin = std::io::stdin();
// lockすることでバッファーを専有する
let stdin = stdin.lock();
// <C-c>が入力されるまで受け付ける
for line in stdin.lines() {
println!("{:?}", line);
}
}
std::io::stdin().lock()
はNG
fn main() {
let stdin = std::io::stdin().lock(); // NG
// let stdin = std::io::stdin();
// let stdin = stdin.lock();
for line in stdin.lines() {
println!("{:?}", line);
}
}
/*
error[E0716]: temporary value dropped while borrowed
|
4 | let stdin = std::io::stdin().lock(); // NG
| ^^^^^^^^^^^^^^^^ - temporary value is freed at the end of this statement
| |
| creates a temporary which is freed while still in use
...
7 | for line in stdin.lines() {
| ----- borrow later used here
|
= note: consider using a `let` binding to create a longer lived value
*/
pub fn lock(&self) -> StdinLock<'_>