scanln
The inverse of println
scanln
is a macro that reads a line from stdin
, removes the trailing
newline (if present), and leaves other trailing whitespace intact. It
expands to an expression that returns a String
, so it can be assigned to
a variable or used directly. The arguments to scanln
are exactly the same as
those to the print
macro and can be used to generate a prompt.
scanln
is very simple to use:
rust
let input = scanln!("> ");
This results in a prompt that looks like this (where _
is the cursor):
```
_ ```
When the user presses enter, a String
containing their input will be stored in
input
.
There are more examples demonstrating usage in the examples
directory, which
can be run with cargo
.