-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Remove std::old_io from Standard Input in tutorial book #23800
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b795ab8
Replaced old_io with io in standard input tutorial page
984c53e
Small type: "oio" -> "io"
840779a
Got rid of corefn. It does not compile.
d0feff9
A few more errors found from reading everything over a few times and …
67ebd8e
Quick explanation of the mutable reference since it hasn't been intro…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,10 +5,11 @@ we haven't seen before. Here's a simple program that reads some input, | |
and then prints it back out: | ||
|
||
```{rust,ignore} | ||
corefn main() { | ||
fn main() { | ||
println!("Type something!"); | ||
|
||
let input = std::old_io::stdin().read_line().ok().expect("Failed to read line"); | ||
let mut input = String::new(); | ||
std::io::stdin().read_line(&mut input).ok().expect("Failed to read line"); | ||
|
||
println!("{}", input); | ||
} | ||
|
@@ -17,19 +18,24 @@ corefn main() { | |
Let's go over these chunks, one by one: | ||
|
||
```{rust,ignore} | ||
std::old_io::stdin(); | ||
let mut input = String::new(); | ||
``` | ||
|
||
This creates an empty string which will act as a buffer when we read from stdin. | ||
|
||
```{rust,ignore} | ||
std::io::stdin(); | ||
``` | ||
|
||
This calls a function, `stdin()`, that lives inside the `std::old_io` module. As | ||
This calls a function, `stdin()`, that lives inside the `std::io` module. As | ||
you can imagine, everything in `std` is provided by Rust, the 'standard | ||
library.' We'll talk more about the module system later. | ||
|
||
Since writing the fully qualified name all the time is annoying, we can use | ||
the `use` statement to import it in: | ||
|
||
```{rust} | ||
# #![feature(old_io)] | ||
use std::old_io::stdin; | ||
use std::io::stdin; | ||
|
||
stdin(); | ||
``` | ||
|
@@ -38,21 +44,21 @@ However, it's considered better practice to not import individual functions, but | |
to import the module, and only use one level of qualification: | ||
|
||
```{rust} | ||
# #![feature(old_io)] | ||
use std::old_io; | ||
use std::io; | ||
|
||
old_io::stdin(); | ||
io::stdin(); | ||
``` | ||
|
||
Let's update our example to use this style: | ||
|
||
```{rust,ignore} | ||
use std::old_io; | ||
use std::io; | ||
|
||
fn main() { | ||
println!("Type something!"); | ||
|
||
let input = old_io::stdin().read_line().ok().expect("Failed to read line"); | ||
let mut input = String::new(); | ||
io::stdin().read_line(&mut input).ok().expect("Failed to read line"); | ||
|
||
println!("{}", input); | ||
} | ||
|
@@ -61,11 +67,15 @@ fn main() { | |
Next up: | ||
|
||
```{rust,ignore} | ||
.read_line() | ||
.read_line(&mut input) | ||
``` | ||
|
||
The `read_line()` method can be called on the result of `stdin()` to return | ||
a full line of input. Nice and easy. | ||
The `read_line()` method can be called on the result of `stdin()` to write | ||
a full line of input to its argument, a String buffer. We pass `input` as a | ||
mutable reference, since `read_line()` will be modifying the buffer. Every | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. References aren't mentioned until chapter 3.3 of the book. So it might be nice to say something like you did on L118, so newbies (like myself) don't get confused. |
||
character up to and including the first newline encountered is written to | ||
`input`, which grows dynamically as needed. `read_line()` returns the number | ||
of bytes written to `input`, which we do not make use of here. | ||
|
||
```{rust,ignore} | ||
.ok().expect("Failed to read line"); | ||
|
@@ -100,16 +110,16 @@ though, we _know_ that `x` has a `Value`, but `match` forces us to handle | |
the `missing` case. This is what we want 99% of the time, but sometimes, we | ||
know better than the compiler. | ||
|
||
Likewise, `read_line()` does not return a line of input. It _might_ return a | ||
line of input, though it might also fail to do so. This could happen if our program | ||
isn't running in a terminal, but as part of a cron job, or some other context | ||
where there's no standard input. Because of this, `read_line` returns a type | ||
very similar to our `OptionalInt`: an `IoResult<T>`. We haven't talked about | ||
`IoResult<T>` yet because it is the *generic* form of our `OptionalInt`. | ||
Until then, you can think of it as being the same thing, just for any type – | ||
not just `i32`s. | ||
Likewise, `read_line()` does not return the number of bytes written. It _might_ | ||
return the number of bytes written, though it might also fail to do so. This | ||
could happen if our program isn't running in a terminal, but as part of a cron | ||
job, or some other context where there's no standard input. Because of this, | ||
`read_line` returns a type very similar to our `OptionalInt`: a `Result<T>`. | ||
We haven't talked about `Result<T>` yet because it is the *generic* form of | ||
our `OptionalInt`. Until then, you can think of it as being the same thing, | ||
just for any type – not just `i32`s. | ||
|
||
Rust provides a method on these `IoResult<T>`s called `ok()`, which does the | ||
Rust provides a method on these `Result<T>`s called `ok()`, which does the | ||
same thing as our `match` statement but assumes that we have a valid value. | ||
We then call `expect()` on the result, which will terminate our program if we | ||
don't have a valid value. In this case, if we can't get input, our program | ||
|
@@ -124,12 +134,13 @@ work with. | |
Back to the code we were working on! Here's a refresher: | ||
|
||
```{rust,ignore} | ||
use std::old_io; | ||
use std::io; | ||
|
||
fn main() { | ||
println!("Type something!"); | ||
|
||
let input = old_io::stdin().read_line().ok().expect("Failed to read line"); | ||
let mut input = String::new(); | ||
io::stdin().read_line(&mut input).ok().expect("Failed to read line"); | ||
|
||
println!("{}", input); | ||
} | ||
|
@@ -139,17 +150,19 @@ With long lines like this, Rust gives you some flexibility with the whitespace. | |
We _could_ write the example like this: | ||
|
||
```{rust,ignore} | ||
use std::old_io; | ||
use std::io; | ||
|
||
fn main() { | ||
println!("Type something!"); | ||
|
||
let mut input = String::new(); | ||
|
||
// here, we'll show the types at each step | ||
|
||
let input = old_io::stdin() // std::old_io::stdio::StdinReader | ||
.read_line() // IoResult<String> | ||
.ok() // Option<String> | ||
.expect("Failed to read line"); // String | ||
io::stdin() // std::io::Stdin | ||
.read_line(&mut input) // Result<usize> | ||
.ok() // Option<usize> | ||
.expect("Failed to read line"); // usize | ||
|
||
println!("{}", input); | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.