@@ -1503,6 +1503,112 @@ building our guessing game, but we need to know how to do one last thing first:
1503
1503
get input from the keyboard. You can't have a guessing game without the ability
1504
1504
to guess!
1505
1505
1506
+ ## Standard Input
1507
+
1508
+ Getting input from the keyboard is pretty easy, but uses some things
1509
+ we haven't seen before. Here's a simple program that reads some input,
1510
+ and then prints it back out:
1511
+
1512
+ ``` {rust,ignore}
1513
+ fn main() {
1514
+ println!("Type something!");
1515
+
1516
+ let input = std::io::stdin().read_line().ok().expect("Failed to read line");
1517
+
1518
+ println!("{}", input);
1519
+ }
1520
+ ```
1521
+
1522
+ Let's go over these chunks, one by one:
1523
+
1524
+ ``` {rust}
1525
+ std::io::stdin();
1526
+ ```
1527
+
1528
+ This calls a function, ` stdin() ` , that lives inside the ` std::io ` module. As
1529
+ you can imagine, everything in ` std ` is provided by Rust, the 'standard
1530
+ library.' We'll talk more about the module system later.
1531
+
1532
+ Since writing the fully qualified name all the time is annoying, we can use
1533
+ the ` use ` statement to import it in:
1534
+
1535
+ ``` {rust}
1536
+ use std::io::stdin;
1537
+
1538
+ stdin();
1539
+ ```
1540
+
1541
+ However, it's considered better practice to not import individual functions, but
1542
+ to import the module, and only use one level of qualification:
1543
+
1544
+ ``` {rust}
1545
+ use std::io;
1546
+
1547
+ io::stdin();
1548
+ ```
1549
+
1550
+ Let's update our example to use this style:
1551
+
1552
+ ``` {rust,ignore}
1553
+ use std::io;
1554
+
1555
+ fn main() {
1556
+ println!("Type something!");
1557
+
1558
+ let input = io::stdin().read_line().ok().expect("Failed to read line");
1559
+
1560
+ println!("{}", input);
1561
+ }
1562
+ ```
1563
+
1564
+ Next up:
1565
+
1566
+ ``` {rust,ignore}
1567
+ .read_line()
1568
+ ```
1569
+
1570
+ The ` read_line() ` method can be called on the result of ` stdin() ` to return
1571
+ a full line of input. Nice and easy.
1572
+
1573
+ ``` {rust,ignore}
1574
+ .ok().expect("Failed to read line");
1575
+ ```
1576
+
1577
+ Here's the thing: reading a line from standard input could fail. For example,
1578
+ if this program isn't running in a terminal, but is running as part of a cron
1579
+ job, or some other context where there's no standard input. So Rust expects us
1580
+ to handle this case. Given that we plan on always running this program in a
1581
+ terminal, we use the ` ok() ` method to tell Rust that we're expecting everything
1582
+ to be just peachy, and the ` expect() ` method on that result to give an error
1583
+ message if our expectation goes wrong.
1584
+
1585
+ We will cover the exact details of how all of this works later in the Guide.
1586
+ For now, this is all you need.
1587
+
1588
+ With long lines like this, Rust gives you some flexibility with the whitespace.
1589
+ We _ could_ write the example like this:
1590
+
1591
+ ``` {rust,ignore}
1592
+ use std::io;
1593
+
1594
+ fn main() {
1595
+ println!("Type something!");
1596
+
1597
+ let input = io::stdin()
1598
+ .read_line()
1599
+ .ok()
1600
+ .expect("Failed to read line");
1601
+
1602
+ println!("{}", input);
1603
+ }
1604
+ ```
1605
+
1606
+ Sometimes, this makes things more readable. Sometimes, less. Use your judgement
1607
+ here.
1608
+
1609
+ That's all you need to get basic input from the standard input! It's not too
1610
+ complicated, but there are a number of small parts.
1611
+
1506
1612
## Guessing Game: complete
1507
1613
1508
1614
At this point, you have successfully built the Guessing Game! Congratulations!
0 commit comments