Existing answers I've found are all based on from_str
(such as Reading in user input from console once efficiently), but apparently from_str(x)
has changed into x.parse()
in Rust 1.0. As a newbie, it's not obvious how the original solution should be adapted taking this change into account.
As of Rust 1.0, what is the easiest way to get an integer input from the user?
Probably the easiest part would be to use the text_io crate and write:
If you need to read more than one value simultaneously, you might need to use Rust nightly.
Here are a few possibilities (Rust 1.7):
These spare you the ceremony of pattern matching without depending on extra libraries.
Here is a version with all optional type annotations and error handling which may be useful for beginners like me:
parse
is more or less the same; it’sread_line
that’s unpleasant now.