Rust - Standard IO
https://doc.rust-lang.org/std/io/index.html
Using the std::io library provides you with a number of useful features, including the ability to accept user input.
https://doc.rust-lang.org/stable/std/io/struct.Stdin.html
The stdin
function returns an instance of std::io::Stdin
, which is a type that represents a handle to the standard input for your terminal.
https://doc.rust-lang.org/stable/std/io/struct.Stdin.html#method.read_line
The line .read_line(&mut guess)
calls the read_line method on the standard input handle to get input from the user.
We are passing &mut guess as the argument to read_line to tell it what string to store the user input in. The full job of read_line is to take whatever the user types into standard input and append that into a string (without overwriting its contents), so we therefore pass that string as an argument. The string argument needs to be mutable so the method can change the string’s content.
read_line
puts whatever the user enters into the string we pass to it, but it also returns a Result
value. Result
is an enumeration
, often called an enum, which is a type that can be in one of multiple possible states. We call each possible state a variant.
https://doc.rust-lang.org/stable/std/result/enum.Result.html
The purpose of these Result types is to encode error-handling information.
https://doc.rust-lang.org/stable/book/ch06-00-enums.html
Result’s variants are Ok
and Err
. The Ok
variant indicates the operation was successful, and inside Ok
is the successfully generated value. The Err
variant means the operation failed, and Err
contains information about how or why the operation failed.
Values of the Result
type, like values of any type, have methods defined on them. An instance of Result
has an expect method that you can call. If this instance of Result
is an Err
value, expect will cause the program to crash and display the message that you passed as an argument to expect. If the read_line
method returns an Err
, it would likely be the result of an error coming from the underlying operating system. If this instance of Result
is an Ok
value, expect
will take the return value that Ok
is holding and return just that value to you so you can use it. In this case, that value is the number of bytes in the user’s input.
https://doc.rust-lang.org/stable/std/result/enum.Result.html#method.expect
If you don’t call expect, the program will compile, but you’ll get a warning:
/home/h/.cargo/bin/cargo build --color=always --message-format=json-diagnostic-rendered-ansi
Compiling guessing_game v0.1.0 (/home/h/Downloads/GitRepositories/programming-playground/rust-playground/guessing_game)
warning: unused `Result` that must be used
--> src/main.rs:17:9
|
17 | / io::stdin()
18 | | .read_line(&mut guess);
| |___________________________________^
|
= note: `#[warn(unused_must_use)]` on by default
= note: this `Result` may be an `Err` variant, which should be handled
warning: 1 warning emitted
Finished dev [unoptimized + debuginfo] target(s) in 0.32s
Process finished with exit code 0
Rust warns that you haven’t used the Result
value returned from read_line
, indicating that the program hasn’t handled a possible error.
The right way to suppress the warning is to actually write error-handling code, but in our case we just want to crash this program when a problem occurs, so we can use expect
.