How can I read user input in Rust?

Rust 1.x (see documentation): use std::io; use std::io::prelude::*; fn main() { let stdin = io::stdin(); for line in stdin.lock().lines() { println!(“{}”, line.unwrap()); } } Rust 0.10–0.12 (see documentation): use std::io; fn main() { for line in io::stdin().lines() { print!(“{}”, line.unwrap()); } } Rust 0.9 (see 0.9 documentation): use std::io; use std::io::buffered::BufferedReader; fn main() { let …

Read more

Scanner is skipping nextLine() after using next() or nextFoo()?

That’s because the Scanner.nextInt method does not read the newline character in your input created by hitting “Enter,” and so the call to Scanner.nextLine returns after reading that newline. You will encounter the similar behaviour when you use Scanner.nextLine after Scanner.next() or any Scanner.nextFoo method (except nextLine itself). Workaround: Either put a Scanner.nextLine call after …

Read more