rust - Read a single line from stdin -
how can read single line stdin in rust? i'm asking equivalent of fgets()
in c.
let line = ...; println!("you entered: {}", line);
note: read how read user input in rust?, asks how read multiple lines, want 1 line. saw how read single string standard input?, i'm not sure if behaves fgets()
or sscanf("%s",...)
.
in how read user input in rust? answer, can see how iterate on lines.
use std::io::bufread; let stdin = io::stdin(); line in stdin.lock().lines() { println!("{}", line.unwrap()); }
you can manually iterate without for-loop:
let stdin = io::stdin(); let mut iterator = stdin.lock().lines(); let line1 = iterator.next().unwrap().unwrap(); let line2 = iterator.next().unwrap().unwrap();
you cannot one-liner want. following gets single line (and same answer in how read single string standard input?):
let stdin = std::io::stdin(); let line1 = stdin.lock().lines().next().unwrap().unwrap();
you can use text_io
crate super simple input:
#[macro_use] extern crate text_io; fn main() { // reads until \n encountered let line: string = read!("{}\n"); }
Comments
Post a Comment