scanln
The inverse of println
scanln
is a macro that reads a line from stdin
, removes the trailing
newline (if present), and leaves other whitespace intact. It expands to an
expression that returns a String
, so it can be assigned to a variable or
used directly. scanln
may take arguments like those to the print
macro,
which can be used to generate a prompt.
rust
let input = scanln!("> ");
This results in a prompt that looks like this (where _
represents where the
user will start typing):
```
_ ```
rust
let input = scanln!();
Result:
_
rust
let input = scanln!("{}", 0);
Result:
0_