In How to read user input in Rust?, si può vedere come iterare su tutte le linee:
use std::io::{self, BufRead};
fn main() {
let stdin = io::stdin();
for line in stdin.lock().lines() {
println!("{}", line.unwrap());
}
}
È anche possibile scorrere manualmente senza un ciclo for:
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();
}
Non è possibile ottenere una battuta a Fai quello che vuoi. Ma il seguente ottiene una sola linea (ed è esattamente la stessa risposta come 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();
}
È anche possibile utilizzare il text_io
cassa per super semplice ingresso:
#[macro_use] extern crate text_io;
fn main() {
// reads until a \n is encountered
let line: String = read!("{}\n");
}
fonte
2015-05-12 09:15:25
Che dire di [Stdin :: read_line] (http://doc.rust-lang.org/nightly/std/io/struct.Stdin.html#method.read_line)? – Gerstmann
@Gerstmann: questa funzione è piuttosto strana da usare secondo me. –