1+
2+ [ TOC]
3+
14# Guessing Game
25
3- Let's jump into Rust with a hands-on project! We’ll implement a classic
4- beginner programming problem: the guessing game. Here’s how it works: Our
5- program will generate a random integer between one and a hundred. It will then
6- prompt us to enter a guess. Upon entering our guess, it will tell us if we’re
7- too low or too high. Once we guess correctly, it will congratulate us.
6+ Let's jump into Rust with a hands-on project! This chapter will introduce you to
7+ a few common Rust concepts by showing how you would use them in a real program.
8+ You'll learn about ` let ` , ` match ` , methods, associated functions, using
9+ external crates, and more! Following chapters will explore these ideas in more
10+ detail.
11+
12+ We’re going to implement a classic beginner programming problem: the guessing
13+ game. Here’s how it works: Our program will generate a random integer between
14+ one and a hundred. It will then prompt us to enter a guess. Upon entering our
15+ guess, it will tell us if we’re too low or too high. Once we guess correctly,
16+ it will congratulate us.
817
918## Setting Up a New Project
1019
@@ -89,11 +98,15 @@ There’s a lot here! Let’s go over it, bit by bit.
8998use std::io;
9099```
91100
92- We’ll need to take user input and then print the result as output. As such, we
93- need the ` io ` library from the standard library. Rust only imports a few things
94- by default into every program, [ the ‘prelude’] [ prelude ] . If it’s not in the
95- prelude, you’ll have to ` use ` it directly. Using the ` std::io ` library gets
96- you a number of useful ` io ` -related things, so that's what we've done here.
101+ We’ll need to take user input and then print the result as output, and for that
102+ functonality we need to import the ` io ` (input/output) library from the
103+ standard library (which is known as ` std ` ).
104+
105+ By default, Rust only imports a few things into every program in [ the
106+ * prelude* ] ( prelude ) . If it’s not in the prelude, you’ll have to import it into
107+ your program explicitly with a ` use ` statement. Using the ` std::io ` library
108+ gets you a number of useful ` io ` -related things, including the functionality to
109+ accept user input.
97110
98111[ prelude ] : ../std/prelude/index.html
99112
@@ -115,15 +128,17 @@ As we learned in Chapter 1, `println!()` is a macro that prints a string to the
115128screen. This is just a prompt stating what the game is and requesting input from
116129the user.
117130
118- ### Variable Bindings
131+ ### Storing Values with Variable Bindings
132+
133+ Next we need to store the user input.
119134
120135``` rust,ignore
121136let mut guess = String::new();
122137```
123138
124139Now we’re getting interesting! There’s a lot going on in this little line.
125- The first thing to notice is that this is a let statement, which is
126- used to create what are called ‘variable bindings’. Here's an example:
140+ The first thing to notice is that this is a ` let ` statement, which is
141+ used to create ‘variable bindings’. Here's another example:
127142
128143``` rust,ignore
129144let foo = bar;
@@ -153,15 +168,19 @@ bound to: `String::new()`.
153168
154169[ string ] : ../std/string/struct.String.html
155170
156- The ` :: ` syntax in the ` ::new() ` line indicates that ` new() ` is an * associated function * of
157- a particular type. That is to say, it’s associated with ` String ` itself,
158- rather than a particular instance of a ` String ` . Some languages call this a
159- * static method* .
171+ The ` :: ` syntax in the ` ::new() ` line indicates that ` new() ` is an * associated
172+ function * of a particular type. An associated function is a function that is
173+ associated with a type, in this case ` String ` , rather than a particular
174+ instance of a ` String ` . Some languages call this a * static method* .
160175
161176This ` new() ` function creates a new, empty ` String ` .
162177You’ll find a ` new() ` function on many types, as it’s a common name for making
163178a new value of some kind.
164179
180+ So to summarize, the ` let mut guess = String::new(); ` line has created a
181+ mutable binding that is currently bound to a new, empty instance of a ` String ` .
182+ Whew!
183+
165184Let’s move forward:
166185
167186``` rust,ignore
@@ -174,49 +193,44 @@ Remember how we `use`d `std::io` on the first line of the program? We’re now
174193calling an associated function on it. If we didn’t ` use std::io ` , we could
175194have written this line as ` std::io::stdin() ` .
176195
177- This particular function returns a handle to the standard input for your
178- terminal. More specifically, a [ std::io::Stdin] [ iostdin ] .
196+ This particular function returns an instance of [ ` std::io::Stdin ` ] [ iostdin ] ,
197+ which is a type that represents a handle to the standard input for your
198+ terminal.
179199
180200[ iostdin ] : ../std/io/struct.Stdin.html
181201
182- The next part will use this handle to get input from the user:
183-
184- ``` rust,ignore
185- .read_line(&mut guess)
186- ```
187-
188- Here, we call the [ ` read_line() ` ] [ read_line ] method on our handle. We’re also
202+ The next part, ` .read_line(&mut guess) ` , calls the [ ` readline() ` ] [ read_line ]
203+ method on the standard input handle to get input from the user. We’re also
189204passing one argument to ` read_line() ` : ` &mut guess ` .
190205
191206[ read_line ] : ../std/io/struct.Stdin.html#method.read_line
192207
193- Remember how we bound ` guess ` above? We said it was mutable. However,
194- ` read_line ` doesn’t take a ` String ` as an argument: it takes a ` &mut String ` .
195- The ` & ` is the feature of Rust called a ‘reference’, which allows you to have
196- multiple ways to access one piece of data in order to reduce copying.
197- References are a complex feature, as one of Rust’s major selling points is how
198- safe and easy it is to use references. We don’t need to know a lot of those
199- details to finish our program right now, though; Chapter XX will cover references in
200- more detail. For now, all we need to know is that like ` let ` bindings,
201- references are immutable by default. Hence, we need to write ` &mut guess ` ,
202- rather than ` &guess ` to make it mutable.
203-
204- Why does ` read_line() ` take a mutable reference to a string? Its job is
205- to take what the user types into standard input and place that into a
206- string. So it takes that string as an argument, and in order to add
207- the input, that string needs to be mutable.
208-
209- We’re not quite done with this line of code. While it’s
210- a single line of text, it’s only the first part of the single logical line of
211- code. The second part is this method:
208+ The job of ` read_line() ` is to take whatever the user types into standard input
209+ and place that into a string, so it takes that string as an argument. The
210+ string argument needs to be mutable so that the method can change the string's
211+ content by adding the user input.
212+
213+ The ` & ` indicates that this argument is a ` reference ` , which gives you a way to
214+ allow multiple parts of your code to access to one piece of data without
215+ needing to copy that data into memory multiple times. References are a complex
216+ feature, and one of Rust’s major advantages is how safe and easy it is to use
217+ references. We don’t need to know a lot of those details to finish our program
218+ right now, though; Chapter XX will cover references in more detail. For now,
219+ all we need to know is that like ` let ` bindings, references are immutable by
220+ default. Hence, we need to write ` &mut guess ` , rather than ` &guess ` to make it
221+ mutable.
222+
223+ We’re not quite done with this line of code. While it’s a single line of text,
224+ it’s only the first part of the single logical line of code. The second part is
225+ this method:
212226
213227``` rust,ignore
214228.expect("Failed to read line");
215229```
216230
217- When you call a method with the ` .foo() ` syntax, you may introduce a newline
218- and other whitespace. This helps you split up long lines. We _ could_ have
219- written this code as:
231+ When you call a method with the ` .foo() ` syntax, it's often wise to introduce a
232+ newline and other whitespace. This helps you split up long lines. We _ could_
233+ have written this code as:
220234
221235``` rust,ignore
222236io::stdin().read_line(&mut guess).expect("failed to read line");
0 commit comments