@@ -1360,7 +1360,7 @@ while !done {
1360
1360
```
1361
1361
1362
1362
` while ` loops are the correct choice when you're not sure how many times
1363
- you need to loop.
1363
+ you need to loop.
1364
1364
1365
1365
If you need an infinite loop, you may be tempted to write this:
1366
1366
@@ -1650,7 +1650,7 @@ a full line of input. Nice and easy.
1650
1650
.ok().expect("Failed to read line");
1651
1651
```
1652
1652
1653
- Do you remember this code?
1653
+ Do you remember this code?
1654
1654
1655
1655
```
1656
1656
enum OptionalInt {
@@ -1796,6 +1796,21 @@ Excellent! Open up your `src/main.rs` again. We'll be writing all of
1796
1796
our code in this file. We'll talk about multiple-file projects later on in the
1797
1797
guide.
1798
1798
1799
+ Before we move on, let me show you one more Cargo command: ` run ` . ` cargo run `
1800
+ is kind of like ` cargo build ` , but it also then runs the produced exectuable.
1801
+ Try it out:
1802
+
1803
+ ``` {notrust,ignore}
1804
+ $ cargo run
1805
+ Compiling guessing_game v0.1.0 (file:/home/you/projects/guessing_game)
1806
+ Running `target/guessing_game`
1807
+ Hello, world!
1808
+ $
1809
+ ```
1810
+
1811
+ Great! The ` run ` command comes in handy when you need to rapidly iterate on a project.
1812
+ Our game is just such a project, we need to quickly test each iteration before moving on to the next one.
1813
+
1799
1814
## Processing a Guess
1800
1815
1801
1816
Let's get to it! The first thing we need to do for our guessing game is
@@ -1933,19 +1948,19 @@ $
1933
1948
Excellent! Try running our new program a few times:
1934
1949
1935
1950
``` {notrust,ignore}
1936
- $ ./target/guessing_game
1951
+ $ ./target/guessing_game
1937
1952
Guess the number!
1938
1953
The secret number is: 7
1939
1954
Please input your guess.
1940
1955
4
1941
1956
You guessed: 4
1942
- $ ./target/guessing_game
1957
+ $ ./target/guessing_game
1943
1958
Guess the number!
1944
1959
The secret number is: 83
1945
1960
Please input your guess.
1946
1961
5
1947
1962
You guessed: 5
1948
- $ ./target/guessing_game
1963
+ $ ./target/guessing_game
1949
1964
Guess the number!
1950
1965
The secret number is: -29
1951
1966
Please input your guess.
@@ -1986,7 +2001,7 @@ And trying it out:
1986
2001
``` {notrust,ignore}
1987
2002
$ cargo build
1988
2003
Compiling guessing_game v0.1.0 (file:/home/you/projects/guessing_game)
1989
- $ ./target/guessing_game
2004
+ $ ./target/guessing_game
1990
2005
Guess the number!
1991
2006
The secret number is: 57
1992
2007
Please input your guess.
@@ -2022,7 +2037,7 @@ fn main() {
2022
2037
2023
2038
println!("You guessed: {}", input);
2024
2039
2025
- match cmp(input, secret_number) {
2040
+ match cmp(input, secret_number) {
2026
2041
Less => println!("Too small!"),
2027
2042
Greater => println!("Too big!"),
2028
2043
Equal => { println!("You win!"); },
@@ -2106,7 +2121,7 @@ a `String` instead! That's because our `input` variable is coming from the
2106
2121
standard input, and you can guess anything. Try it:
2107
2122
2108
2123
``` {notrust,ignore}
2109
- $ ./target/guessing_game
2124
+ $ ./target/guessing_game
2110
2125
Guess the number!
2111
2126
The secret number is: 73
2112
2127
Please input your guess.
@@ -2257,7 +2272,7 @@ print an error message and return. Let's give this a shot:
2257
2272
``` {notrust,ignore}
2258
2273
$ cargo build
2259
2274
Compiling guessing_game v0.1.0 (file:/home/you/projects/guessing_game)
2260
- $ ./target/guessing_game
2275
+ $ ./target/guessing_game
2261
2276
Guess the number!
2262
2277
The secret number is: 17
2263
2278
Please input your guess.
@@ -2323,7 +2338,7 @@ Let's try it!
2323
2338
``` {notrust,ignore}
2324
2339
$ cargo build
2325
2340
Compiling guessing_game v0.1.0 (file:/home/you/projects/guessing_game)
2326
- $ ./target/guessing_game
2341
+ $ ./target/guessing_game
2327
2342
Guess the number!
2328
2343
The secret number is: 58
2329
2344
Please input your guess.
@@ -2401,7 +2416,7 @@ that `return`? If we give a non-number answer, we'll `return` and quit. Observe:
2401
2416
``` {notrust,ignore}
2402
2417
$ cargo build
2403
2418
Compiling guessing_game v0.1.0 (file:/home/you/projects/guessing_game)
2404
- $ ./target/guessing_game
2419
+ $ ./target/guessing_game
2405
2420
Guess the number!
2406
2421
The secret number is: 59
2407
2422
Please input your guess.
@@ -2534,7 +2549,7 @@ Now we should be good! Let's try:
2534
2549
``` {rust,ignore}
2535
2550
$ cargo build
2536
2551
Compiling guessing_game v0.1.0 (file:/home/you/projects/guessing_game)
2537
- $ ./target/guessing_game
2552
+ $ ./target/guessing_game
2538
2553
Guess the number!
2539
2554
The secret number is: 61
2540
2555
Please input your guess.
@@ -2731,16 +2746,6 @@ mod hello {
2731
2746
2732
2747
This will work:
2733
2748
2734
- ``` {notrust,ignore}
2735
- $ cargo build
2736
- Compiling modules v0.1.0 (file:/home/you/projects/modules)
2737
- $
2738
- ```
2739
-
2740
- Before we move on, let me show you one more Cargo command: ` run ` . ` cargo run `
2741
- is kind of like ` cargo build ` , but it also then runs the produced exectuable.
2742
- Try it out:
2743
-
2744
2749
``` {notrust,ignore}
2745
2750
$ cargo run
2746
2751
Compiling modules v0.1.0 (file:/home/steve/tmp/modules)
@@ -3647,14 +3652,14 @@ In order to truly understand this error, we have to learn a few new concepts:
3647
3652
All of our references so far have been to variables we've created on the stack.
3648
3653
In Rust, the simplest way to allocate heap variables is using a * box* . To
3649
3654
create a box, use the ` box ` keyword:
3650
-
3655
+
3651
3656
``` {rust}
3652
3657
let x = box 5i;
3653
3658
```
3654
3659
3655
3660
This allocates an integer ` 5 ` on the heap, and creates a binding ` x ` that
3656
3661
refers to it.. The great thing about boxed pointers is that we don't have to
3657
- manually free this allocation! If we write
3662
+ manually free this allocation! If we write
3658
3663
3659
3664
``` {rust}
3660
3665
{
@@ -4189,7 +4194,7 @@ the match:
4189
4194
4190
4195
``` {rust,ignore}
4191
4196
let x = inverse(25.0f64);
4192
- println!("{}", x + 2.0f64); // error: binary operation `+` cannot be applied
4197
+ println!("{}", x + 2.0f64); // error: binary operation `+` cannot be applied
4193
4198
// to type `core::result::Result<f64,collections::string::String>`
4194
4199
```
4195
4200
@@ -4700,4 +4705,3 @@ fail.
4700
4705
# Macros
4701
4706
4702
4707
# Unsafe
4703
-
0 commit comments