Replace all uses of bash syntax highlighting

Since none of these are actually bash scripts :)

Changed all the instances in the src files to text, which, since my PR
to mdbook got accepted, will still do code background colors but no
syntax highlighting.

Fixes #295 and #327.
This commit is contained in:
Carol (Nichols || Goulding)
2016-11-14 12:18:48 -05:00
parent dbe2c1aa0b
commit 3c38b59ab6
25 changed files with 96 additions and 96 deletions
+5 -5
View File
@@ -19,7 +19,7 @@ Building the book requires [mdBook] >= v0.0.13. To get it:
[mdBook]: https://github.com/azerupi/mdBook [mdBook]: https://github.com/azerupi/mdBook
```bash ```
$ cargo install mdbook $ cargo install mdbook
``` ```
@@ -27,7 +27,7 @@ $ cargo install mdbook
To build the book, type: To build the book, type:
```bash ```
$ mdbook build $ mdbook build
``` ```
@@ -35,20 +35,20 @@ The output will be in the `book` subdirectory. To check it out, open it in
your web browser. your web browser.
_Firefox:_ _Firefox:_
```bash ```
$ firefox book/index.html # Linux $ firefox book/index.html # Linux
$ open -a "Firefox" book/index.html # OS X $ open -a "Firefox" book/index.html # OS X
``` ```
_Chrome:_ _Chrome:_
```bash ```
$ google-chrome book/index.html # Linux $ google-chrome book/index.html # Linux
$ open -a "Google Chrome" book/index.html # OS X $ open -a "Google Chrome" book/index.html # OS X
``` ```
To run the tests: To run the tests:
```bash ```
$ mdbook test $ mdbook test
``` ```
+1 -1
View File
@@ -174,7 +174,7 @@ Any such expression always has the `unit` type.
The precedence of Rust binary operators is ordered as follows, going from The precedence of Rust binary operators is ordered as follows, going from
strong to weak: strong to weak:
```bash ```text
as : as :
* / % * / %
+ - + -
+5 -5
View File
@@ -16,14 +16,14 @@ with `$` are typically showing the output of the previous command.
If you're on Linux or a Mac, all you need to do is open a terminal and type If you're on Linux or a Mac, all you need to do is open a terminal and type
this: this:
```bash ```text
$ curl https://sh.rustup.rs -sSf | sh $ curl https://sh.rustup.rs -sSf | sh
``` ```
This will download a script and start the installation. You may be prompted for This will download a script and start the installation. You may be prompted for
your password. If it all goes well, you’ll see this appear: your password. If it all goes well, you’ll see this appear:
```bash ```text
Rust is installed now. Great! Rust is installed now. Great!
``` ```
@@ -48,7 +48,7 @@ installation page](https://www.rust-lang.org/install.html) for other options.
Uninstalling Rust is as easy as installing it. From your shell, run Uninstalling Rust is as easy as installing it. From your shell, run
the uninstall script: the uninstall script:
```bash ```text
$ rustup self uninstall $ rustup self uninstall
``` ```
@@ -56,14 +56,14 @@ $ rustup self uninstall
If you've got Rust installed, you can open up a shell, and type this: If you've got Rust installed, you can open up a shell, and type this:
```bash ```text
$ rustc --version $ rustc --version
``` ```
You should see the version number, commit hash, and commit date in a format You should see the version number, commit hash, and commit date in a format
similar to this for the latest stable version at the time you install: similar to this for the latest stable version at the time you install:
```bash ```text
rustc x.y.z (abcabcabc yyyy-mm-dd) rustc x.y.z (abcabcabc yyyy-mm-dd)
``` ```
+13 -13
View File
@@ -19,7 +19,7 @@ the following commands to make a directory for this particular project:
Linux and Mac: Linux and Mac:
```bash ```text
$ mkdir ~/projects $ mkdir ~/projects
$ cd ~/projects $ cd ~/projects
$ mkdir hello_world $ mkdir hello_world
@@ -55,7 +55,7 @@ fn main() {
Save the file, and go back to your terminal window. On Linux or OSX, enter the Save the file, and go back to your terminal window. On Linux or OSX, enter the
following commands: following commands:
```bash ```text
$ rustc main.rs $ rustc main.rs
$ ./main $ ./main
Hello, world! Hello, world!
@@ -121,7 +121,7 @@ Before running a Rust program, you have to compile it. You can use the Rust
compiler by entering the `rustc` command and passing it the name of your source compiler by entering the `rustc` command and passing it the name of your source
file, like this: file, like this:
```bash ```text
$ rustc main.rs $ rustc main.rs
``` ```
@@ -130,7 +130,7 @@ If you come from a C or C++ background, you'll notice that this is similar to
executable, which you can see on Linux or OSX by entering the `ls` command in executable, which you can see on Linux or OSX by entering the `ls` command in
your shell as follows: your shell as follows:
```bash ```text
$ ls $ ls
main main.rs main main.rs
``` ```
@@ -147,7 +147,7 @@ This shows we have two files: the source code, with the `.rs` extension, and the
executable (`main.exe` on Windows, `main` everywhere else). All that's left to executable (`main.exe` on Windows, `main` everywhere else). All that's left to
do from here is run the `main` or `main.exe` file, like this: do from here is run the `main` or `main.exe` file, like this:
```bash ```text
$ ./main # or .\main.exe on Windows $ ./main # or .\main.exe on Windows
``` ```
@@ -189,7 +189,7 @@ itself, if you used the official installers as covered in the Installation
chapter. If you installed Rust through some other means, you can check if you chapter. If you installed Rust through some other means, you can check if you
have Cargo installed by typing the following into your terminal: have Cargo installed by typing the following into your terminal:
```bash ```text
$ cargo --version $ cargo --version
``` ```
@@ -205,7 +205,7 @@ decided to put your code):
Linux and Mac: Linux and Mac:
```bash ```text
$ cd ~/projects $ cd ~/projects
``` ```
@@ -217,7 +217,7 @@ Windows:
And then on any operating system run: And then on any operating system run:
```bash ```text
$ cargo new hello_cargo --bin $ cargo new hello_cargo --bin
$ cd hello_cargo $ cd hello_cargo
``` ```
@@ -304,7 +304,7 @@ Cargo by moving your code into the `src` directory and creating an appropriate
Now let's look at what's different about building and running your Hello World Now let's look at what's different about building and running your Hello World
program through Cargo! To do so, enter the following commands: program through Cargo! To do so, enter the following commands:
```bash ```text
$ cargo build $ cargo build
Compiling hello_cargo v0.1.0 (file:///projects/hello_cargo) Compiling hello_cargo v0.1.0 (file:///projects/hello_cargo)
``` ```
@@ -312,7 +312,7 @@ $ cargo build
This should have created an executable file in `target/debug/hello_cargo` (or This should have created an executable file in `target/debug/hello_cargo` (or
`target\debug\hello_cargo.exe` on Windows), which you can run with this command: `target\debug\hello_cargo.exe` on Windows), which you can run with this command:
```bash ```text
$ ./target/debug/hello_cargo # or .\target\debug\hello_cargo.exe on Windows $ ./target/debug/hello_cargo # or .\target\debug\hello_cargo.exe on Windows
Hello, world! Hello, world!
``` ```
@@ -339,7 +339,7 @@ We just built a project with `cargo build` and ran it with
`./target/debug/hello_cargo`, but we can also use `cargo run` to compile `./target/debug/hello_cargo`, but we can also use `cargo run` to compile
and then run: and then run:
```bash ```text
$ cargo run $ cargo run
Running `target/debug/hello_cargo` Running `target/debug/hello_cargo`
Hello, world! Hello, world!
@@ -351,7 +351,7 @@ it just ran the binary. If you had modified your source code, Cargo would have
rebuilt the project before running it, and you would have seen something like rebuilt the project before running it, and you would have seen something like
this: this:
```bash ```text
$ cargo run $ cargo run
Compiling hello_cargo v0.1.0 (file:///projects/hello_cargo) Compiling hello_cargo v0.1.0 (file:///projects/hello_cargo)
Running `target/debug/hello_cargo` Running `target/debug/hello_cargo`
@@ -393,7 +393,7 @@ tooling you’ll use for the rest of your Rust career. In fact, you can get
started with virtually all Rust projects you want to work started with virtually all Rust projects you want to work
on with the following commands: on with the following commands:
```bash ```text
$ git clone someurl.com/someproject $ git clone someurl.com/someproject
$ cd someproject $ cd someproject
$ cargo build $ cargo build
+12 -12
View File
@@ -17,7 +17,7 @@ game will print congratulations and exit.
To set up a new project, go to the *projects* directory that you created in To set up a new project, go to the *projects* directory that you created in
Chapter 1, and make a new project using Cargo, like so: Chapter 1, and make a new project using Cargo, like so:
```bash ```text
$ cargo new guessing_game --bin $ cargo new guessing_game --bin
$ cd guessing_game $ cd guessing_game
``` ```
@@ -57,7 +57,7 @@ fn main() {
Now let’s compile this “Hello, world!” program and run it in the same step Now let’s compile this “Hello, world!” program and run it in the same step
using the `cargo run` command: using the `cargo run` command:
```bash ```text
$ cargo run $ cargo run
Compiling guessing_game v0.1.0 (file:///projects/guessing_game) Compiling guessing_game v0.1.0 (file:///projects/guessing_game)
Running `target/debug/guessing_game` Running `target/debug/guessing_game`
@@ -281,7 +281,7 @@ entered into standard input.
If we don’t call `expect`, the program will compile, but we’ll get a warning: If we don’t call `expect`, the program will compile, but we’ll get a warning:
```bash ```text
$ cargo build $ cargo build
Compiling guessing_game v0.1.0 (file:///projects/guessing_game) Compiling guessing_game v0.1.0 (file:///projects/guessing_game)
src/main.rs:10:5: 10:39 warning: unused result which must be used, src/main.rs:10:5: 10:39 warning: unused result which must be used,
@@ -324,7 +324,7 @@ This code would print out `x = 5 and y = 10`.
Let’s test the first part of the guessing game. You can run it using `cargo run`: Let’s test the first part of the guessing game. You can run it using `cargo run`:
```bash ```text
$ cargo run $ cargo run
Compiling guessing_game v0.1.0 (file:///projects/guessing_game) Compiling guessing_game v0.1.0 (file:///projects/guessing_game)
Running `target/debug/guessing_game` Running `target/debug/guessing_game`
@@ -382,7 +382,7 @@ version 0.3.14.”
Now, without changing any of the code, let’s build the project, as shown in Now, without changing any of the code, let’s build the project, as shown in
Listing 2-2: Listing 2-2:
```bash ```text
$ cargo build $ cargo build
Updating registry `https://github.com/rust-lang/crates.io-index` Updating registry `https://github.com/rust-lang/crates.io-index`
Downloading rand v0.3.14 Downloading rand v0.3.14
@@ -421,7 +421,7 @@ doesn't recompile that either. With nothing to do, it simply exits. If you open
up the *src/main.rs* file, make a trivial change, then save it and build again, up the *src/main.rs* file, make a trivial change, then save it and build again,
you’ll only see one line of output: you’ll only see one line of output:
```bash ```text
$ cargo build $ cargo build
Compiling guessing_game v0.1.0 (file:///projects/guessing_game) Compiling guessing_game v0.1.0 (file:///projects/guessing_game)
``` ```
@@ -464,7 +464,7 @@ But by default, Cargo will only look for versions larger than `0.3.0` and
smaller than `0.4.0`. If the `rand` crate has released two new versions, smaller than `0.4.0`. If the `rand` crate has released two new versions,
`0.3.15` and `0.4.0`, you would see the following if you ran `cargo update`: `0.3.15` and `0.4.0`, you would see the following if you ran `cargo update`:
```bash ```text
$ cargo update $ cargo update
Updating registry `https://github.com/rust-lang/crates.io-index` Updating registry `https://github.com/rust-lang/crates.io-index`
Updating rand v0.3.14 -> v0.3.15 Updating rand v0.3.14 -> v0.3.15
@@ -564,7 +564,7 @@ the answer as soon as it starts!
Try running the program a few times: Try running the program a few times:
```bash ```text
$ cargo run $ cargo run
Compiling guessing_game v0.1.0 (file:///projects/guessing_game) Compiling guessing_game v0.1.0 (file:///projects/guessing_game)
Running `target/debug/guessing_game` Running `target/debug/guessing_game`
@@ -675,7 +675,7 @@ look at the last arm in this particular scenario.
However, the code in Listing 2-4 won’t compile yet. Let’s try it: However, the code in Listing 2-4 won’t compile yet. Let’s try it:
```bash ```text
$ cargo build $ cargo build
Compiling guessing_game v0.1.0 (file:///projects/guessing_game) Compiling guessing_game v0.1.0 (file:///projects/guessing_game)
error[E0308]: mismatched types error[E0308]: mismatched types
@@ -793,7 +793,7 @@ and `expect` will return the number that we want from the `Ok` value.
Let’s run the program now! Let’s run the program now!
```bash ```text
$ cargo run $ cargo run
Compiling guessing_game v0.1.0 (file:///projects/guessing_game) Compiling guessing_game v0.1.0 (file:///projects/guessing_game)
Running `target/guessing_game` Running `target/guessing_game`
@@ -868,7 +868,7 @@ the `parse` discussion in “Comparing the Guesses” on page XX: if the user
enters a non-number answer, the program will crash. The user can take advantage enters a non-number answer, the program will crash. The user can take advantage
of that in order to quit, as shown here: of that in order to quit, as shown here:
```bash ```text
$ cargo run $ cargo run
Compiling guessing_game v0.1.0 (file:///projects/guessing_game) Compiling guessing_game v0.1.0 (file:///projects/guessing_game)
Running `target/guessing_game` Running `target/guessing_game`
@@ -985,7 +985,7 @@ might encounter!
Now everything in the program should work as expected. Let’s try it by running Now everything in the program should work as expected. Let’s try it by running
`cargo run`: `cargo run`:
```bash ```text
$ cargo run $ cargo run
Compiling guessing_game v0.1.0 (file:///projects/guessing_game) Compiling guessing_game v0.1.0 (file:///projects/guessing_game)
Running `target/guessing_game` Running `target/guessing_game`
+4 -4
View File
@@ -27,7 +27,7 @@ fn main() {
Save and run the program using `cargo run`. You should receive an error Save and run the program using `cargo run`. You should receive an error
message, as shown in this output: message, as shown in this output:
```bash ```text
$ cargo run $ cargo run
Compiling variables v0.0.1 (file:///projects/variables) Compiling variables v0.0.1 (file:///projects/variables)
error[E0384]: re-assignment of immutable variable `x` error[E0384]: re-assignment of immutable variable `x`
@@ -82,7 +82,7 @@ fn main() {
When we run this program, we get the following: When we run this program, we get the following:
```bash ```text
$ cargo run $ cargo run
Compiling variables v0.1.0 (file:///projects/variables) Compiling variables v0.1.0 (file:///projects/variables)
Running `target/debug/variables` Running `target/debug/variables`
@@ -166,7 +166,7 @@ repeating `let x =`, taking the original value and adding `1` so the value of
previous value and multiplying it by `2` to give `x` a final value of `12`. previous value and multiplying it by `2` to give `x` a final value of `12`.
When you run this program, it will output the following: When you run this program, it will output the following:
```bash ```text
$ cargo run $ cargo run
Compiling variables v0.1.0 (file:///projects/variables) Compiling variables v0.1.0 (file:///projects/variables)
Running `target/debug/variables` Running `target/debug/variables`
@@ -204,7 +204,7 @@ spaces = spaces.len();
we’ll get a compile-time error because we’re not allowed to mutate a variable’s we’ll get a compile-time error because we’re not allowed to mutate a variable’s
type: type:
```bash ```text
error[E0308]: mismatched types error[E0308]: mismatched types
--> src/main.rs:3:14 --> src/main.rs:3:14
| |
+2 -2
View File
@@ -20,7 +20,7 @@ If we don’t add the type annotation here, Rust will display the following
error, which means the compiler needs more information from us to know which error, which means the compiler needs more information from us to know which
possible type we want to use: possible type we want to use:
```bash ```text
error[E0282]: unable to infer enough type information about `_` error[E0282]: unable to infer enough type information about `_`
--> src/main.rs:2:5 --> src/main.rs:2:5
| |
@@ -347,7 +347,7 @@ fn main() {
Running this code using `cargo run` produces the following result: Running this code using `cargo run` produces the following result:
```bash ```text
$ cargo run $ cargo run
Compiling arrays v0.1.0 (file:///projects/arrays) Compiling arrays v0.1.0 (file:///projects/arrays)
Running `target/debug/arrays` Running `target/debug/arrays`
+6 -6
View File
@@ -38,7 +38,7 @@ Let’s start a new binary project named *functions* to explore functions
further. Place the `another_function` example in *src/main.rs* and run it. You further. Place the `another_function` example in *src/main.rs* and run it. You
should see the following output: should see the following output:
```bash ```text
$ cargo run $ cargo run
Compiling functions v0.1.0 (file:///projects/functions) Compiling functions v0.1.0 (file:///projects/functions)
Running `target/debug/functions` Running `target/debug/functions`
@@ -69,7 +69,7 @@ fn another_function(x: i32) {
Try running this program; you should get the following output: Try running this program; you should get the following output:
```bash ```text
$ cargo run $ cargo run
Compiling functions v0.1.0 (file:///projects/functions) Compiling functions v0.1.0 (file:///projects/functions)
Running `target/debug/functions` Running `target/debug/functions`
@@ -111,7 +111,7 @@ Let’s try running this code. Replace the program currently in your *function*
project’s *src/main.rs* file with the preceding example, and run it using project’s *src/main.rs* file with the preceding example, and run it using
`cargo run`: `cargo run`:
```bash ```text
$ cargo run $ cargo run
Compiling functions v0.1.0 (file:///projects/functions) Compiling functions v0.1.0 (file:///projects/functions)
Running `target/debug/functions` Running `target/debug/functions`
@@ -169,7 +169,7 @@ fn main() {
When you run this program, you’ll get an error like this: When you run this program, you’ll get an error like this:
```bash ```text
$ cargo run $ cargo run
Compiling functions v0.1.0 (file:///projects/functions) Compiling functions v0.1.0 (file:///projects/functions)
error: expected expression, found statement (`let`) error: expected expression, found statement (`let`)
@@ -253,7 +253,7 @@ function—just the number `5` by itself. That’s a perfectly valid function in
Rust. Note that the function’s return type is specified, too, as `-> i32`. Try Rust. Note that the function’s return type is specified, too, as `-> i32`. Try
running this code; the output should look like this: running this code; the output should look like this:
```bash ```text
$ cargo run $ cargo run
Compiling functions v0.1.0 (file:///projects/functions) Compiling functions v0.1.0 (file:///projects/functions)
Running `target/debug/functions` Running `target/debug/functions`
@@ -309,7 +309,7 @@ fn plus_one(x: i32) -> i32 {
Running this code produces an error, as follows: Running this code produces an error, as follows:
```bash ```text
error[E0269]: not all control paths return a value error[E0269]: not all control paths return a value
--> src/main.rs:7:1 --> src/main.rs:7:1
| |
+8 -8
View File
@@ -44,7 +44,7 @@ the next bit of code.
Try running this code; you should see the following output: Try running this code; you should see the following output:
```bash ```text
$ cargo run $ cargo run
Compiling branches v0.1.0 (file:///projects/branches) Compiling branches v0.1.0 (file:///projects/branches)
Running `target/debug/branches` Running `target/debug/branches`
@@ -60,7 +60,7 @@ let number = 7;
Run the program again, and look at the output: Run the program again, and look at the output:
```bash ```text
$ cargo run $ cargo run
Compiling branches v0.1.0 (file:///projects/branches) Compiling branches v0.1.0 (file:///projects/branches)
Running `target/debug/branches` Running `target/debug/branches`
@@ -86,7 +86,7 @@ fn main() {
The `if` condition evaluates to a value of `3` this time, and Rust throws an The `if` condition evaluates to a value of `3` this time, and Rust throws an
error: error:
```bash ```text
Compiling branches v0.1.0 (file:///projects/branches) Compiling branches v0.1.0 (file:///projects/branches)
error[E0308]: mismatched types error[E0308]: mismatched types
--> src/main.rs:4:8 --> src/main.rs:4:8
@@ -148,7 +148,7 @@ fn main() {
This program has four possible paths it can take. After running it, you should This program has four possible paths it can take. After running it, you should
see the following output: see the following output:
```bash ```text
$ cargo run $ cargo run
Compiling branches v0.1.0 (file:///projects/branches) Compiling branches v0.1.0 (file:///projects/branches)
Running `target/debug/branches` Running `target/debug/branches`
@@ -193,7 +193,7 @@ Listing 3-4: Assigning the result of an `if` expression to a variable
The `number` variable will be bound to a value based on the outcome of the `if` The `number` variable will be bound to a value based on the outcome of the `if`
expression. Run this code to see what happens: expression. Run this code to see what happens:
```bash ```text
$ cargo run $ cargo run
Compiling branches v0.1.0 (file:///projects/branches) Compiling branches v0.1.0 (file:///projects/branches)
Running `target/debug/branches` Running `target/debug/branches`
@@ -228,7 +228,7 @@ When we run this code, we’ll get an error. The `if` and `else` arms have value
types that are incompatible, and Rust indicates exactly where to find the types that are incompatible, and Rust indicates exactly where to find the
problem in the program: problem in the program:
```bash ```text
Compiling branches v0.1.0 (file:///projects/branches) Compiling branches v0.1.0 (file:///projects/branches)
error[E0308]: if and else have incompatible types error[E0308]: if and else have incompatible types
--> src/main.rs:4:18 --> src/main.rs:4:18
@@ -280,7 +280,7 @@ When we run this program, we’ll see `again!` printed over and over continuousl
until we stop the program manually. Most terminals support a keyboard shortcut, until we stop the program manually. Most terminals support a keyboard shortcut,
ctrl-C, to halt a program that is stuck in a continual loop. Give it a try: ctrl-C, to halt a program that is stuck in a continual loop. Give it a try:
```bash ```text
$ cargo run $ cargo run
Compiling loops v0.1.0 (file:///projects/loops) Compiling loops v0.1.0 (file:///projects/loops)
Running `target/debug/loops` Running `target/debug/loops`
@@ -363,7 +363,7 @@ Here, the code counts up through the elements in the array. It starts at index
when `index < 5` is no longer true). Running this code will print out every when `index < 5` is no longer true). Running this code will print out every
element in the array: element in the array:
```bash ```text
$ cargo run $ cargo run
Compiling loops v0.1.0 (file:///projects/loops) Compiling loops v0.1.0 (file:///projects/loops)
Running `target/debug/loops` Running `target/debug/loops`
+1 -1
View File
@@ -327,7 +327,7 @@ println!("{}", s1);
You’ll get an error like this: You’ll get an error like this:
```bash ```text
5:22 error: use of moved value: `s1` [E0382] 5:22 error: use of moved value: `s1` [E0382]
println!("{}", s1); println!("{}", s1);
^~ ^~
+5 -5
View File
@@ -92,7 +92,7 @@ fn change(some_string: &String) {
Here’s the error: Here’s the error:
```bash ```text
error: cannot borrow immutable borrowed content `*some_string` as mutable error: cannot borrow immutable borrowed content `*some_string` as mutable
--> error.rs:8:5 --> error.rs:8:5
| |
@@ -140,7 +140,7 @@ let r2 = &mut s;
Here’s the error: Here’s the error:
```bash ```text
error[E0499]: cannot borrow `s` as mutable more than once at a time error[E0499]: cannot borrow `s` as mutable more than once at a time
--> borrow_twice.rs:5:19 --> borrow_twice.rs:5:19
| |
@@ -195,7 +195,7 @@ let r3 = &mut s; // BIG PROBLEM
Here’s the error: Here’s the error:
```bash ```text
error[E0502]: cannot borrow `s` as mutable because it is also borrowed as immutable error[E0502]: cannot borrow `s` as mutable because it is also borrowed as immutable
--> borrow_thrice.rs:6:19 --> borrow_thrice.rs:6:19
| |
@@ -247,7 +247,7 @@ fn dangle() -> &String {
Here’s the error: Here’s the error:
```bash ```text
error[E0106]: missing lifetime specifier error[E0106]: missing lifetime specifier
--> dangle.rs:5:16 --> dangle.rs:5:16
| |
@@ -266,7 +266,7 @@ This error message refers to a feature we haven’t learned about yet:
the parts about lifetimes, the message does contain the key to why this code is the parts about lifetimes, the message does contain the key to why this code is
a problem: a problem:
```bash ```text
this function's return type contains a borrowed value, but there is no value this function's return type contains a borrowed value, but there is no value
for it to be borrowed from. for it to be borrowed from.
``` ```
+1 -1
View File
@@ -246,7 +246,7 @@ fn main() {
Here’s the compiler error: Here’s the compiler error:
```bash ```text
17:6 error: cannot borrow `s` as mutable because it is also borrowed as 17:6 error: cannot borrow `s` as mutable because it is also borrowed as
immutable [E0502] immutable [E0502]
s.clear(); // Error! s.clear(); // Error!
+6 -6
View File
@@ -92,7 +92,7 @@ fn area(length: u32, width: u32) -> u32 {
Let's try running this program with `cargo run`: Let's try running this program with `cargo run`:
```bash ```text
The area of the rectangle is 1500 square pixels. The area of the rectangle is 1500 square pixels.
``` ```
@@ -226,7 +226,7 @@ fn main() {
If we run this, we get an error with this core message: If we run this, we get an error with this core message:
```bash ```text
error: the trait bound `Rectangle: std::fmt::Display` is not satisfied error: the trait bound `Rectangle: std::fmt::Display` is not satisfied
``` ```
@@ -242,7 +242,7 @@ want and structs do not have a provided implementation of `Display`.
If we keep reading the errors, though, we'll find this helpful note: If we keep reading the errors, though, we'll find this helpful note:
```bash ```text
note: `Rectangle` cannot be formatted with the default formatter; try using note: `Rectangle` cannot be formatted with the default formatter; try using
`:?` instead if you are using a format string `:?` instead if you are using a format string
``` ```
@@ -256,13 +256,13 @@ code.
Let's try running with this change and... drat. We still get an error: Let's try running with this change and... drat. We still get an error:
```bash ```text
error: the trait bound `Rectangle: std::fmt::Debug` is not satisfied error: the trait bound `Rectangle: std::fmt::Debug` is not satisfied
``` ```
Again, though, the compiler has given us a helpful note! Again, though, the compiler has given us a helpful note!
```bash ```text
note: `Rectangle` cannot be formatted using `:?`; if it is defined in your note: `Rectangle` cannot be formatted using `:?`; if it is defined in your
crate, add `#[derive(Debug)]` or manually implement it crate, add `#[derive(Debug)]` or manually implement it
``` ```
@@ -289,7 +289,7 @@ fn main() {
At this point, if we run this program, we won't get any errors and we'll see the At this point, if we run this program, we won't get any errors and we'll see the
following output: following output:
```bash ```text
rect1 is Rectangle { length: 50, width: 30 } rect1 is Rectangle { length: 50, width: 30 }
``` ```
+1 -1
View File
@@ -139,7 +139,7 @@ fn main() {
We want to see this output, since both of `rect2`'s dimensions are smaller than We want to see this output, since both of `rect2`'s dimensions are smaller than
`rect1`'s, but `rect3` is wider than `rect1`: `rect1`'s, but `rect3` is wider than `rect1`:
```bash ```text
Can rect1 hold rect2? true Can rect1 hold rect2? true
Can rect1 hold rect3? false Can rect1 hold rect3? false
``` ```
+1 -1
View File
@@ -99,7 +99,7 @@ let sum = x + y;
If we run this code, we get an error message like this: If we run this code, we get an error message like this:
```bash ```text
error[E0277]: the trait bound `i8: std::ops::Add<std::option::Option<i8>>` is not satisfied error[E0277]: the trait bound `i8: std::ops::Add<std::option::Option<i8>>` is not satisfied
--> -->
| |
+1 -1
View File
@@ -265,7 +265,7 @@ fn plus_one(x: Option<i32>) -> Option<i32> {
We didn't handle the `None` case, so this will cause a bug. Luckily, it's a bug We didn't handle the `None` case, so this will cause a bug. Luckily, it's a bug
Rust knows how to catch. If we try to compile this code, we'll get this error: Rust knows how to catch. If we try to compile this code, we'll get this error:
```bash ```text
error[E0004]: non-exhaustive patterns: `None` not covered error[E0004]: non-exhaustive patterns: `None` not covered
--> -->
| |
+4 -4
View File
@@ -12,7 +12,7 @@ call our library `communicator`. By default, cargo will create a library unless
another type of project is specified, so if we leave off the `--bin` option another type of project is specified, so if we leave off the `--bin` option
that we’ve been using so far our project will be a library: that we’ve been using so far our project will be a library:
```bash ```text
$ cargo new communicator $ cargo new communicator
$ cd communicator $ cd communicator
``` ```
@@ -248,7 +248,7 @@ Now, everything should compile successfully, though you’ll get a few warnings.
Remember to use `cargo build` instead of `cargo run` since we have a library Remember to use `cargo build` instead of `cargo run` since we have a library
crate rather than a binary crate: crate rather than a binary crate:
```bash ```text
$ cargo build $ cargo build
Compiling communicator v0.1.0 (file:///projects/communicator) Compiling communicator v0.1.0 (file:///projects/communicator)
@@ -332,7 +332,7 @@ fn connect() {
When we try to `cargo build`, we’ll get this error: When we try to `cargo build`, we’ll get this error:
```bash ```text
$ cargo build $ cargo build
Compiling communicator v0.1.0 (file:///projects/communicator) Compiling communicator v0.1.0 (file:///projects/communicator)
error: cannot declare a new module at this location error: cannot declare a new module at this location
@@ -378,7 +378,7 @@ previously, we can do what the note suggests:
Here are commands to carry out these steps: Here are commands to carry out these steps:
```bash ```text
$ mkdir src/network $ mkdir src/network
$ mv src/network.rs src/network/mod.rs $ mv src/network.rs src/network/mod.rs
$ mv src/server.rs src/network $ mv src/server.rs src/network
@@ -7,7 +7,7 @@ able to build our project, but we still get some warning messages about the
`client::connect`, `network::connect`, and `network::server::connect` functions `client::connect`, `network::connect`, and `network::server::connect` functions
not being used: not being used:
```bash ```text
warning: function is never used: `connect`, #[warn(dead_code)] on by default warning: function is never used: `connect`, #[warn(dead_code)] on by default
src/client.rs:1:1 src/client.rs:1:1
| |
@@ -60,7 +60,7 @@ Our binary crate right now just calls our library’s `connect` function from th
`client` module. However, invoking `cargo build` will now give us an error `client` module. However, invoking `cargo build` will now give us an error
after the warnings: after the warnings:
```bash ```text
error: module `client` is private error: module `client` is private
--> src/main.rs:4:5 --> src/main.rs:4:5
| |
@@ -103,7 +103,7 @@ mod network;
The `pub` goes right before `mod`. Let’s try building again: The `pub` goes right before `mod`. Let’s try building again:
```bash ```text
<warnings> <warnings>
error: function `connect` is private error: function `connect` is private
--> src/main.rs:4:5 --> src/main.rs:4:5
@@ -125,7 +125,7 @@ pub fn connect() {
And run `cargo build` again: And run `cargo build` again:
```bash ```text
warning: function is never used: `connect`, #[warn(dead_code)] on by default warning: function is never used: `connect`, #[warn(dead_code)] on by default
--> src/network/mod.rs:1:1 --> src/network/mod.rs:1:1
| |
@@ -163,7 +163,7 @@ mod server;
And compile: And compile:
```bash ```text
warning: function is never used: `connect`, #[warn(dead_code)] on by default warning: function is never used: `connect`, #[warn(dead_code)] on by default
--> src/network/mod.rs:1:1 --> src/network/mod.rs:1:1
| |
@@ -194,7 +194,7 @@ pub mod network;
Now if we compile, that warning is gone: Now if we compile, that warning is gone:
```bash ```text
warning: function is never used: `connect`, #[warn(dead_code)] on by default warning: function is never used: `connect`, #[warn(dead_code)] on by default
--> src/network/server.rs:1:1 --> src/network/server.rs:1:1
| |
+2 -2
View File
@@ -182,7 +182,7 @@ mod tests {
Run the tests by invoking the `cargo test` command: Run the tests by invoking the `cargo test` command:
```bash ```text
$ cargo test $ cargo test
Compiling communicator v0.1.0 (file:///projects/communicator) Compiling communicator v0.1.0 (file:///projects/communicator)
error[E0433]: failed to resolve. Use of undeclared type or module `client` error[E0433]: failed to resolve. Use of undeclared type or module `client`
@@ -254,7 +254,7 @@ mod tests {
If we run `cargo test` again, the test will pass and the first part of the test If we run `cargo test` again, the test will pass and the first part of the test
result output will be: result output will be:
```bash ```text
$ cargo test $ cargo test
Compiling communicator v0.1.0 (file:///projects/communicator) Compiling communicator v0.1.0 (file:///projects/communicator)
Running target/debug/communicator-92007ddb5330fa5a Running target/debug/communicator-92007ddb5330fa5a
+2 -2
View File
@@ -120,7 +120,7 @@ fn element() -> String {
Trying to compile this will result in the following error: Trying to compile this will result in the following error:
```bash ```text
error: cannot move out of indexed content [--explain E0507] error: cannot move out of indexed content [--explain E0507]
|> |>
4 |> list[1] 4 |> list[1]
@@ -143,7 +143,7 @@ v.push(6);
Compiling this will give us this error: Compiling this will give us this error:
```bash ```text
error: cannot borrow `v` as mutable because it is also borrowed as immutable error: cannot borrow `v` as mutable because it is also borrowed as immutable
[--explain E0502] [--explain E0502]
|> |>
+3 -3
View File
@@ -308,7 +308,7 @@ that `s` will be "Зд".
What would happen if we did `&hello[0..1]`? The answer: it will panic at What would happen if we did `&hello[0..1]`? The answer: it will panic at
runtime, in the same way that accessing an invalid index in a vector does: runtime, in the same way that accessing an invalid index in a vector does:
```bash ```text
thread 'main' panicked at 'index 0 and/or 1 in `Здравствуйте` do not lie on thread 'main' panicked at 'index 0 and/or 1 in `Здравствуйте` do not lie on
character boundary', ../src/libcore/str/mod.rs:1694 character boundary', ../src/libcore/str/mod.rs:1694
``` ```
@@ -327,7 +327,7 @@ for c in "नमस्ते".chars() {
This code will print: This code will print:
```bash ```text
न न
म म
स स
@@ -348,7 +348,7 @@ for b in "नमस्ते".bytes() {
This code will print the 18 bytes that make up this `String`, starting with: This code will print the 18 bytes that make up this `String`, starting with:
```bash ```text
224 224
164 164
168 168
+1 -1
View File
@@ -114,7 +114,7 @@ for (key, value) in &map {
This will print: This will print:
```bash ```text
1: hello 1: hello
2: world 2: world
``` ```
@@ -31,7 +31,7 @@ fn main() {
If you run it, you'll see something like this: If you run it, you'll see something like this:
```bash ```text
$ cargo run $ cargo run
Compiling panic v0.1.0 (file:///projects/panic) Compiling panic v0.1.0 (file:///projects/panic)
Finished debug [unoptimized + debuginfo] target(s) in 0.25 secs Finished debug [unoptimized + debuginfo] target(s) in 0.25 secs
@@ -74,7 +74,7 @@ In order to protect your program from this sort of vulnerability, if you try to
read an element at an index that doesn't exist, Rust will stop execution and read an element at an index that doesn't exist, Rust will stop execution and
refuse to continue with an invalid value. Let's try it and see: refuse to continue with an invalid value. Let's try it and see:
```bash ```text
$ cargo run $ cargo run
Compiling panic v0.1.0 (file:///projects/panic) Compiling panic v0.1.0 (file:///projects/panic)
Finished debug [unoptimized + debuginfo] target(s) in 0.27 secs Finished debug [unoptimized + debuginfo] target(s) in 0.27 secs
@@ -94,7 +94,7 @@ That's what the next line, the `note` is about. If we set the `RUST_BACKTRACE`
environment variable, we'll get a backtrace of exactly how the error happend. environment variable, we'll get a backtrace of exactly how the error happend.
Let's try that. Listing 9-1 shows the output: Let's try that. Listing 9-1 shows the output:
```bash ```text
$ RUST_BACKTRACE=1 cargo run $ RUST_BACKTRACE=1 cargo run
Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs
Running `target/debug/panic` Running `target/debug/panic`
@@ -71,7 +71,7 @@ turn our error into a `panic!` instead, by calling the macro. And since we
haven't created that file yet, we'll see a message indicating as such when we haven't created that file yet, we'll see a message indicating as such when we
print the error value: print the error value:
```bash ```text
thread 'main' panicked at 'There was a problem opening the file: Error { repr: thread 'main' panicked at 'There was a problem opening the file: Error { repr:
Os { code: 2, message: "No such file or directory" } }', src/main.rs:8 Os { code: 2, message: "No such file or directory" } }', src/main.rs:8
``` ```
@@ -333,7 +333,7 @@ switch this example to use `?`. /Carol -->
When we compile this, we get the following error message: When we compile this, we get the following error message:
```bash ```text
error[E0308]: mismatched types error[E0308]: mismatched types
--> -->
| |
+1 -1
View File
@@ -36,7 +36,7 @@ example, a program with only this:
Will give a compiler error: Will give a compiler error:
```bash ```text
src/main.rs:1:1: 1:27 error: expected item after doc comment src/main.rs:1:1: 1:27 error: expected item after doc comment
src/main.rs:1 /// What am I documenting? src/main.rs:1 /// What am I documenting?
^~~~~~~~~~~~~~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~