Skip to content

Commit 17bb4af

Browse files
committed
Guide: Standard input
1 parent 35e2134 commit 17bb4af

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

src/doc/guide.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,112 @@ loop
966966

967967
break/continue
968968

969+
## Standard Input
970+
971+
Getting input from the keyboard is pretty easy, but uses some things
972+
we haven't seen before. Here's a simple program that reads some input,
973+
and then prints it back out:
974+
975+
```{rust,ignore}
976+
fn main() {
977+
println!("Type something!");
978+
979+
let input = std::io::stdin().read_line().ok().expect("Failed to read line");
980+
981+
println!("{}", input);
982+
}
983+
```
984+
985+
Let's go over these chunks, one by one:
986+
987+
```{rust}
988+
std::io::stdin();
989+
```
990+
991+
This calls a function, `stdin()`, that lives inside the `std::io` module. As
992+
you can imagine, everything in `std` is provided by Rust, the 'standard
993+
library.' We'll talk more about the module system later.
994+
995+
Since writing the fully qualified name all the time is annoying, we can use
996+
the `use` statement to import it in:
997+
998+
```{rust}
999+
use std::io::stdin;
1000+
1001+
stdin();
1002+
```
1003+
1004+
However, it's considered better practice to not import individual functions, but
1005+
to import the module, and only use one level of qualification:
1006+
1007+
```{rust}
1008+
use std::io;
1009+
1010+
io::stdin();
1011+
```
1012+
1013+
Let's update our example to use this style:
1014+
1015+
```{rust,ignore}
1016+
use std::io;
1017+
1018+
fn main() {
1019+
println!("Type something!");
1020+
1021+
let input = io::stdin().read_line().ok().expect("Failed to read line");
1022+
1023+
println!("{}", input);
1024+
}
1025+
```
1026+
1027+
Next up:
1028+
1029+
```{rust,ignore}
1030+
.read_line()
1031+
```
1032+
1033+
The `read_line()` method can be called on the result of `stdin()` to return
1034+
a full line of input. Nice and easy.
1035+
1036+
```{rust,ignore}
1037+
.ok().expect("Failed to read line");
1038+
```
1039+
1040+
Here's the thing: reading a line from standard input could fail. For example,
1041+
if this program isn't running in a terminal, but is running as part of a cron
1042+
job, or some other context where there's no standard input. So Rust expects us
1043+
to handle this case. Given that we plan on always running this program in a
1044+
terminal, we use the `ok()` method to tell Rust that we're expecting everything
1045+
to be just peachy, and the `expect()` method on that result to give an error
1046+
message if our expectation goes wrong.
1047+
1048+
We will cover the exact details of how all of this works later in the Guide.
1049+
For now, this is all you need.
1050+
1051+
With long lines like this, Rust gives you some flexibility with the whitespace.
1052+
We _could_ write the example like this:
1053+
1054+
```{rust,ignore}
1055+
use std::io;
1056+
1057+
fn main() {
1058+
println!("Type something!");
1059+
1060+
let input = io::stdin()
1061+
.read_line()
1062+
.ok()
1063+
.expect("Failed to read line");
1064+
1065+
println!("{}", input);
1066+
}
1067+
```
1068+
1069+
Sometimes, this makes things more readable. Sometimes, less. Use your judgement
1070+
here.
1071+
1072+
That's all you need to get basic input from the standard input! It's not too
1073+
complicated, but there are a number of small parts.
1074+
9691075
## Guessing Game: complete
9701076

9711077
At this point, you have successfully built the Guessing Game! Congratulations!

0 commit comments

Comments
 (0)