How to read user input in Rust?

前端 未结 10 1695
情歌与酒
情歌与酒 2021-01-31 07:50

Editor\'s note: This question refers to parts of Rust that predate Rust 1.0, but the general concept is still valid in Rust 1.0.

I intend to

10条回答
  •  滥情空心
    2021-01-31 08:09

    Editor's note: This answer predates Rust 1.0. Please see the other answers for modern solutions.

    In Rust 0.4, use the ReaderUtil trait to access the read_line function. Note that you need to explicitly cast the value to a trait type, eg, reader as io::ReaderUtil

    fn main() {
        let mut allLines = ~[];
        let reader = io::stdin();
    
        while !reader.eof() {
                allLines.push((reader as io::ReaderUtil).read_line());
        }
    
        for allLines.each |line| {
                io::println(fmt!("%s", *line));
        }
    }
    

提交回复
热议问题