Skip to content
Merged
4 changes: 4 additions & 0 deletions 2018-edition/book.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[book]
title = "The Rust Programming Language"
author = "Steve Klabnik and Carol Nichols, with Contributions from the Rust Community"

[output.html]
additional-css = ["ferris.css"]
additional-js = ["ferris.js"]
33 changes: 33 additions & 0 deletions 2018-edition/ferris.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
body.light .does_not_compile,
body.light .panics,
body.light .not_desired_behavior,
body.rust .does_not_compile,
body.rust .panics,
body.rust .not_desired_behavior {
background: #fff1f1;
}

body.coal .does_not_compile,
body.coal .panics,
body.coal .not_desired_behavior,
body.navy .does_not_compile,
body.navy .panics,
body.navy .not_desired_behavior,
body.ayu .does_not_compile,
body.ayu .panics,
body.ayu .not_desired_behavior {
background: #501f21;
}

.ferris {
position: absolute;
z-index: 99;
right: 5px;
top: 30px;
width: 10%;
height: auto;
}

.ferris-explain {
width: 100px;
}
51 changes: 51 additions & 0 deletions 2018-edition/ferris.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
var ferrisTypes = [
{
attr: 'does_not_compile',
title: 'This code does not compile!'
},
{
attr: 'panics',
title: 'This code panics!'
},
{
attr: 'unsafe',
title: 'This code block contains unsafe code.'
},
{
attr: 'not_desired_behavior',
title: 'This code does not produce the desired behavior.'
}
]

document.addEventListener('DOMContentLoaded', () => {
for (var ferrisType of ferrisTypes) {
attachFerrises(ferrisType)
}
})

function attachFerrises (type) {
var elements = document.getElementsByClassName(type.attr)

for (var codeBlock of elements) {
var lines = codeBlock.textContent.split(/\r|\r\n|\n/).length - 1;

if (lines >= 4) {
attachFerris(codeBlock, type)
}
}
}

function attachFerris (element, type) {
var a = document.createElement('a')
a.setAttribute('href', 'ch00-00-introduction.html#ferris')
a.setAttribute('target', '_blank')

var img = document.createElement('img')
img.setAttribute('src', 'img/ferris/' + type.attr + '.svg')
img.setAttribute('title', type.title)
img.className = 'ferris'

a.appendChild(img)

element.parentElement.insertBefore(a, element)
}
15 changes: 13 additions & 2 deletions 2018-edition/src/ch00-00-introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,25 @@ There is no wrong way to read this book: if you want to skip ahead, go for it!
You might have to jump back to earlier chapters if you experience any
confusion. But do whatever works for you.

<span id="ferris"></span>

An important part of the process of learning Rust is learning how to read the
error messages the compiler displays: these will guide you toward working code.
As such, we’ll provide many examples of code that doesn’t compile along with
the error message the compiler will show you in each situation. Know that if
you enter and run a random example, it may not compile! Make sure you read the
surrounding text to see whether the example you’re trying to run is meant to
error. In most situations, we’ll lead you to the correct version of any code
that doesn’t compile.
error. Ferris will also help you distinguish code that isn't meant to work:

| Ferris | Meaning |
|------------------------------------------------------------------------|--------------------------------------------------|
| <img src="img/ferris/does_not_compile.svg" class="ferris-explain"> | This code does not compile! |
| <img src="img/ferris/panics.svg" class="ferris-explain"> | This code panics! |
| <img src="img/ferris/unsafe.svg" class="ferris-explain"> | This code block contains unsafe code. |
| <img src="img/ferris/not_desired_behavior.svg" class="ferris-explain"> | This code does not produce the desired behavior. |

In most situations, we’ll lead you to the correct version of any code that
doesn’t compile.

## Source Code

Expand Down
2 changes: 1 addition & 1 deletion 2018-edition/src/ch02-00-guessing-game-tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ will explain.

<span class="filename">Filename: src/main.rs</span>

```rust,ignore
```rust,ignore,does_not_compile
extern crate rand;

use std::io;
Expand Down
4 changes: 2 additions & 2 deletions 2018-edition/src/ch03-01-variables-and-mutability.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ code with the following code that won’t compile just yet:

<span class="filename">Filename: src/main.rs</span>

```rust,ignore
```rust,ignore,does_not_compile
fn main() {
let x = 5;
println!("The value of x is: {}", x);
Expand Down Expand Up @@ -206,7 +206,7 @@ from having to come up with different names, such as `spaces_str` and
`spaces_num`; instead, we can reuse the simpler `spaces` name. However, if we
try to use `mut` for this, as shown here, we’ll get a compile-time error:

```rust,ignore
```rust,ignore,does_not_compile
let mut spaces = " ";
spaces = spaces.len();
```
Expand Down
2 changes: 1 addition & 1 deletion 2018-edition/src/ch03-02-data-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ compile but exit with an error when it runs:

<span class="filename">Filename: src/main.rs</span>

```rust,ignore
```rust,ignore,panics
fn main() {
let a = [1, 2, 3, 4, 5];
let index = 10;
Expand Down
4 changes: 2 additions & 2 deletions 2018-edition/src/ch03-03-how-functions-work.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ to another variable, as the following code tries to do; you’ll get an error:

<span class="filename">Filename: src/main.rs</span>

```rust,ignore
```rust,ignore,does_not_compile
fn main() {
let x = (let y = 6);
}
Expand Down Expand Up @@ -308,7 +308,7 @@ expression to a statement, we’ll get an error.

<span class="filename">Filename: src/main.rs</span>

```rust,ignore
```rust,ignore,does_not_compile
fn main() {
let x = plus_one(5);
Expand Down
4 changes: 2 additions & 2 deletions 2018-edition/src/ch03-05-control-flow.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ the condition isn’t a `bool`, we’ll get an error. For example:

<span class="filename">Filename: src/main.rs</span>

```rust,ignore
```rust,ignore,does_not_compile
fn main() {
let number = 3;

Expand Down Expand Up @@ -212,7 +212,7 @@ example, we’ll get an error:

<span class="filename">Filename: src/main.rs</span>

```rust,ignore
```rust,ignore,does_not_compile
fn main() {
let condition = true;

Expand Down
2 changes: 1 addition & 1 deletion 2018-edition/src/ch04-01-what-is-ownership.md
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ considers `s1` to no longer be valid and, therefore, Rust doesn’t need to free
anything when `s1` goes out of scope. Check out what happens when you try to
use `s1` after `s2` is created; it won’t work:

```rust,ignore
```rust,ignore,does_not_compile
let s1 = String::from("hello");
let s2 = s1;

Expand Down
8 changes: 4 additions & 4 deletions 2018-edition/src/ch04-02-references-and-borrowing.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ Listing 4-6. Spoiler alert: it doesn’t work!

<span class="filename">Filename: src/main.rs</span>

```rust,ignore
```rust,ignore,does_not_compile
fn main() {
let s = String::from("hello");

Expand Down Expand Up @@ -140,7 +140,7 @@ fail:

<span class="filename">Filename: src/main.rs</span>

```rust,ignore
```rust,ignore,does_not_compile
let mut s = String::from("hello");

let r1 = &mut s;
Expand Down Expand Up @@ -194,7 +194,7 @@ let r2 = &mut s;
A similar rule exists for combining mutable and immutable references. This code
results in an error:

```rust,ignore
```rust,ignore,does_not_compile
let mut s = String::from("hello");

let r1 = &s; // no problem
Expand Down Expand Up @@ -244,7 +244,7 @@ compile-time error:

<span class="filename">Filename: src/main.rs</span>

```rust,ignore
```rust,ignore,does_not_compile
fn main() {
let reference_to_nothing = dangle();
}
Expand Down
2 changes: 1 addition & 1 deletion 2018-edition/src/ch04-03-slices.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ compile time error:

<span class="filename">Filename: src/main.rs</span>

```rust,ignore
```rust,ignore,does_not_compile
fn main() {
let mut s = String::from("hello world");
Expand Down
2 changes: 1 addition & 1 deletion 2018-edition/src/ch05-01-defining-structs.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ itself. We’ll discuss traits in Chapter 10.
>
> <span class="filename">Filename: src/main.rs</span>
>
> ```rust,ignore
> ```rust,ignore,does_not_compile
> struct User {
> username: &str,
> email: &str,
Expand Down
2 changes: 1 addition & 1 deletion 2018-edition/src/ch05-02-example-structs.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ work, however:

<span class="filename">Filename: src/main.rs</span>

```rust,ignore
```rust,ignore,does_not_compile
struct Rectangle {
width: u32,
height: u32,
Expand Down
2 changes: 1 addition & 1 deletion 2018-edition/src/ch06-01-defining-an-enum.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ types, the compiler won’t let us use an `Option<T>` value as if it were
definitely a valid value. For example, this code won’t compile because it’s
trying to add an `i8` to an `Option<i8>`:

```rust,ignore
```rust,ignore,does_not_compile
let x: i8 = 5;
let y: Option<i8> = Some(5);

Expand Down
2 changes: 1 addition & 1 deletion 2018-edition/src/ch06-02-match.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ consistently a user favorite.
There’s one other aspect of `match` we need to discuss. Consider this version
of our `plus_one` function that has a bug and won’t compile:

```rust,ignore
```rust,ignore,does_not_compile
fn plus_one(x: Option<i32>) -> Option<i32> {
match x {
Some(i) => Some(i + 1),
Expand Down
2 changes: 1 addition & 1 deletion 2018-edition/src/ch07-01-mod-and-the-filesystem.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ try anyway so you can see the error. First, change *src/network.rs* to have

<span class="filename">Filename: src/network.rs</span>

```rust,ignore
```rust,ignore,does_not_compile
fn connect() {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ making a *src/main.rs* file containing this code:

<span class="filename">Filename: src/main.rs</span>

```rust,ignore
```rust,ignore,does_not_compile
extern crate communicator;

fn main() {
Expand Down
2 changes: 1 addition & 1 deletion 2018-edition/src/ch07-03-importing-names-with-use.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ be checking any functionality right now. This won’t work yet:

<span class="filename">Filename: src/lib.rs</span>

```rust
```rust,does_not_compile
#[cfg(test)]
mod tests {
#[test]
Expand Down
4 changes: 2 additions & 2 deletions 2018-edition/src/ch08-01-vectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ element for. As an example, let’s see what a program will do if it has a vecto
that holds five elements and then tries to access an element at index 100, as
shown in Listing 8-7:

```rust,should_panic
```rust,should_panic,panics
let v = vec![1, 2, 3, 4, 5];

let does_not_exist = &v[100];
Expand Down Expand Up @@ -169,7 +169,7 @@ scope. That rule applies in Listing 8-8, where we hold an immutable reference to
the first element in a vector and try to add an element to the end, which won’t
work:

```rust,ignore
```rust,ignore,does_not_compile
let mut v = vec![1, 2, 3, 4, 5];

let first = &v[0];
Expand Down
4 changes: 2 additions & 2 deletions 2018-edition/src/ch08-02-strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ string by referencing them by index is a valid and common operation. However,
if you try to access parts of a `String` using indexing syntax in Rust, you’ll
get an error. Consider the invalid code in Listing 8-19:

```rust,ignore
```rust,ignore,does_not_compile
let s1 = String::from("hello");
let h = s1[0];
```
Expand Down Expand Up @@ -298,7 +298,7 @@ each Unicode scalar value in that string takes 2 bytes of storage. Therefore,
an index into the string’s bytes will not always correlate to a valid Unicode
scalar value. To demonstrate, consider this invalid Rust code:

```rust,ignore
```rust,ignore,does_not_compile
let hello = "Здравствуйте";
let answer = &hello[0];
```
Expand Down
4 changes: 2 additions & 2 deletions 2018-edition/src/ch09-01-unrecoverable-errors-with-panic.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Let’s try calling `panic!` in a simple program:
<span class="filename">Filename: src/main.rs</span>
```rust,should_panic
```rust,should_panic,panics
fn main() {
panic!("crash and burn");
}
Expand Down Expand Up @@ -68,7 +68,7 @@ element by index in a vector:

<span class="filename">Filename: src/main.rs</span>

```rust,should_panic
```rust,should_panic,panics
fn main() {
let v = vec![1, 2, 3];
Expand Down
4 changes: 2 additions & 2 deletions 2018-edition/src/ch10-01-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ compile yet, but we’ll fix it later in this chapter.

<span class="filename">Filename: src/main.rs</span>

```rust,ignore
```rust,ignore,does_not_compile
fn largest<T>(list: &[T]) -> T {
let mut largest = list[0];
Expand Down Expand Up @@ -182,7 +182,7 @@ Listing 10-7, our code won’t compile.

<span class="filename">Filename: src/main.rs</span>

```rust,ignore
```rust,ignore,does_not_compile
struct Point<T> {
x: T,
y: T,
Expand Down
2 changes: 1 addition & 1 deletion 2018-edition/src/ch10-02-traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ needing to write out a really long type.
This only works if you have a single type that you're returning, however.
For example, this would *not* work:

```rust,ignore
```rust,ignore,does_not_compile
fn returns_summarizable(switch: bool) -> impl Summary {
if switch {
NewsArticle {
Expand Down
Loading