In How to read user input in Rust? you can see how to iterate over all lines:
use std::io::{self, BufRead};
fn main() {
let stdin = io::stdin();
for line in stdin.lock().lines() {
println!("{}", line.unwrap());
}
}
You can also manually iterate without a for-loop:
use std::io::{self, BufRead};
fn main() {
let stdin = io::stdin();
let mut iterator = stdin.lock().lines();
let line1 = iterator.next().unwrap().unwrap();
let line2 = iterator.next().unwrap().unwrap();
}
You cannot write a one-liner to do what you want. But the following reads a single line (and is exactly the same answer as in How do I read a single String from standard input?):
use std::io::{self, BufRead};
fn main() {
let stdin = io::stdin();
let line1 = stdin.lock().lines().next().unwrap().unwrap();
}
You can also use the text_io
crate for super simple input:
#[macro_use] extern crate text_io;
fn main() {
// reads until a
is encountered
let line: String = read!("{}
");
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…