I intend to make a tokenizer. I need to read every line the user types and stop reading once the user presses Ctrl + D.
I searched around and only found one example on Rust IO which does not even compile. I looked at the io module’s documentation and found that the read_line() function is part of the ReaderUtil interface, but stdin() returns a Reader instead.
The code that I would like would essentially look like the following in C++:
vector<string> readLines () {
vector<string> allLines;
string line;
while (cin >> line) {
allLines.push_back(line);
}
return allLines;
}
This question refers to parts of Rust that predate Rust 1.0, but the general concept is still valid in Rust 1.0.
In Rust 0.4, use the
ReaderUtiltrait to access theread_linefunction. Note that you need to explicitly cast the value to a trait type, for example,reader as io::ReaderUtil: