3 Getting input from the keyboard is pretty easy, but uses some things
4 we haven't seen before. Here's a simple program that reads some input,
5 and then prints it back out:
9 println!("Type something!");
11 let input = std::io::stdin().read_line().ok().expect("Failed to read line");
13 println!("{}", input);
17 Let's go over these chunks, one by one:
23 This calls a function, `stdin()`, that lives inside the `std::io` module. As
24 you can imagine, everything in `std` is provided by Rust, the 'standard
25 library.' We'll talk more about the module system later.
27 Since writing the fully qualified name all the time is annoying, we can use
28 the `use` statement to import it in:
36 However, it's considered better practice to not import individual functions, but
37 to import the module, and only use one level of qualification:
45 Let's update our example to use this style:
51 println!("Type something!");
53 let input = io::stdin().read_line().ok().expect("Failed to read line");
55 println!("{}", input);
65 The `read_line()` method can be called on the result of `stdin()` to return
66 a full line of input. Nice and easy.
69 .ok().expect("Failed to read line");
72 Do you remember this code?
81 let x = OptionalInt::Value(5);
82 let y = OptionalInt::Missing;
85 OptionalInt::Value(n) => println!("x is {}", n),
86 OptionalInt::Missing => println!("x is missing!"),
90 OptionalInt::Value(n) => println!("y is {}", n),
91 OptionalInt::Missing => println!("y is missing!"),
96 We had to match each time to see if we had a value or not. In this case,
97 though, we _know_ that `x` has a `Value`, but `match` forces us to handle
98 the `missing` case. This is what we want 99% of the time, but sometimes, we
99 know better than the compiler.
101 Likewise, `read_line()` does not return a line of input. It _might_ return a
102 line of input, though it might also fail to do so. This could happen if our program
103 isn't running in a terminal, but as part of a cron job, or some other context
104 where there's no standard input. Because of this, `read_line` returns a type
105 very similar to our `OptionalInt`: an `IoResult<T>`. We haven't talked about
106 `IoResult<T>` yet because it is the *generic* form of our `OptionalInt`.
107 Until then, you can think of it as being the same thing, just for any type –
110 Rust provides a method on these `IoResult<T>`s called `ok()`, which does the
111 same thing as our `match` statement but assumes that we have a valid value.
112 We then call `expect()` on the result, which will terminate our program if we
113 don't have a valid value. In this case, if we can't get input, our program
114 doesn't work, so we're okay with that. In most cases, we would want to handle
115 the error case explicitly. `expect()` allows us to give an error message if
118 We will cover the exact details of how all of this works later in the Guide.
119 For now, this gives you enough of a basic understanding to work with.
121 Back to the code we were working on! Here's a refresher:
127 println!("Type something!");
129 let input = io::stdin().read_line().ok().expect("Failed to read line");
131 println!("{}", input);
135 With long lines like this, Rust gives you some flexibility with the whitespace.
136 We _could_ write the example like this:
142 println!("Type something!");
144 // here, we'll show the types at each step
146 let input = io::stdin() // std::io::stdio::StdinReader
147 .read_line() // IoResult<String>
148 .ok() // Option<String>
149 .expect("Failed to read line"); // String
151 println!("{}", input);
155 Sometimes, this makes things more readable – sometimes, less. Use your judgement
158 That's all you need to get basic input from the standard input! It's not too
159 complicated, but there are a number of small parts.