Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compile_fail and should_panic examples are tested wherever possible #2957

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ferris.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
var ferrisTypes = [
{
attr: 'does_not_compile',
attr: 'compile_fail',
title: 'This code does not compile!'
},
{
attr: 'panics',
attr: 'should_panic',
title: 'This code panics!'
},
{
Expand Down
2 changes: 1 addition & 1 deletion src/appendix-01-keywords.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ that uses `match` as its name:

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

```rust,ignore,does_not_compile
```rust,compile_fail
fn match(needle: &str, haystack: &str) -> bool {
haystack.contains(needle)
}
Expand Down
2 changes: 1 addition & 1 deletion src/ch02-00-guessing-game-tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ will explain.

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

```rust,ignore,does_not_compile
```rust,compile_fail,ignore
{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-04/src/main.rs:here}}
```

Expand Down
4 changes: 2 additions & 2 deletions src/ch03-01-variables-and-mutability.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ examine the immutability error.

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

```rust,ignore,does_not_compile
```rust,compile_fail
{{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-01-variables-are-immutable/src/main.rs}}
```

Expand Down Expand Up @@ -174,7 +174,7 @@ 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,does_not_compile
```rust,compile_fail
{{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-05-mut-cant-change-types/src/main.rs:here}}
```

Expand Down
2 changes: 1 addition & 1 deletion src/ch03-02-data-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ Chapter 2, to get an array index from the user:

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

```rust,ignore,panics
```rust,should_panic
{{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-15-invalid-array-access/src/main.rs}}
```

Expand Down
4 changes: 2 additions & 2 deletions src/ch03-03-how-functions-work.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,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,does_not_compile
```rust,compile_fail
{{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-19-statements-vs-expressions/src/main.rs}}
```

Expand Down Expand Up @@ -229,7 +229,7 @@ expression to a statement, we’ll get an error.

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

```rust,ignore,does_not_compile
```rust,compile_fail
{{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-23-statements-dont-return-values/src/main.rs}}
```

Expand Down
4 changes: 2 additions & 2 deletions src/ch03-05-control-flow.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ following code:

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

```rust,ignore,does_not_compile
```rust,compile_fail
{{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-28-if-condition-must-be-bool/src/main.rs}}
```

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

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

```rust,ignore,does_not_compile
```rust,compile_fail
{{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-31-arms-must-return-same-type/src/main.rs}}
```

Expand Down
2 changes: 1 addition & 1 deletion src/ch04-01-what-is-ownership.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ no longer valid. 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,does_not_compile
```rust,compile_fail
{{#rustdoc_include ../listings/ch04-understanding-ownership/no-listing-04-cant-use-after-move/src/main.rs:here}}
```

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

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

```rust,ignore,does_not_compile
```rust,compile_fail
{{#rustdoc_include ../listings/ch04-understanding-ownership/listing-04-06/src/main.rs}}
```

Expand Down Expand Up @@ -103,7 +103,7 @@ create two mutable references to `s` will fail:

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

```rust,ignore,does_not_compile
```rust,compile_fail
{{#rustdoc_include ../listings/ch04-understanding-ownership/no-listing-10-multiple-mut-not-allowed/src/main.rs:here}}
```

Expand Down Expand Up @@ -144,7 +144,7 @@ multiple mutable references, just not *simultaneous* ones:
Rust enforces a similar rule for combining mutable and immutable references.
This code results in an error:

```rust,ignore,does_not_compile
```rust,compile_fail
{{#rustdoc_include ../listings/ch04-understanding-ownership/no-listing-12-immutable-and-mutable-not-allowed/src/main.rs:here}}
```

Expand Down Expand Up @@ -196,7 +196,7 @@ compile-time error:

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

```rust,ignore,does_not_compile
```rust,compile_fail
{{#rustdoc_include ../listings/ch04-understanding-ownership/no-listing-14-dangling-reference/src/main.rs}}
```

Expand All @@ -220,7 +220,7 @@ Let’s take a closer look at exactly what’s happening at each stage of our

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

```rust,ignore,does_not_compile
```rust,compile_fail
{{#rustdoc_include ../listings/ch04-understanding-ownership/no-listing-15-dangling-reference-annotated/src/main.rs:here}}
```

Expand Down
2 changes: 1 addition & 1 deletion src/ch04-03-slices.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ compile-time error:

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

```rust,ignore,does_not_compile
```rust,compile_fail
{{#rustdoc_include ../listings/ch04-understanding-ownership/no-listing-19-slice-error/src/main.rs:here}}
```

Expand Down
2 changes: 1 addition & 1 deletion src/ch05-01-defining-structs.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ unit-like structs.
>
> <!-- CAN'T EXTRACT SEE https://github.com/rust-lang/mdBook/issues/1127 -->
>
> ```rust,ignore,does_not_compile
> ```rust,compile_fail
> struct User {
> username: &str,
> email: &str,
Expand Down
2 changes: 1 addition & 1 deletion src/ch05-02-example-structs.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ previous chapters. This won’t work, however.

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

```rust,ignore,does_not_compile
```rust,compile_fail
{{#rustdoc_include ../listings/ch05-using-structs-to-structure-related-data/listing-05-11/src/main.rs}}
```

Expand Down
2 changes: 1 addition & 1 deletion src/ch06-01-defining-an-enum.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,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,does_not_compile
```rust,compile_fail
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/no-listing-07-cant-use-option-directly/src/main.rs:here}}
```

Expand Down
2 changes: 1 addition & 1 deletion src/ch06-02-match.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,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,does_not_compile
```rust,compile_fail
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/no-listing-10-non-exhaustive-match/src/main.rs:here}}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ in a bit.

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

```rust,ignore,does_not_compile
```rust,compile_fail
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-03/src/lib.rs}}
```

Expand Down Expand Up @@ -109,7 +109,7 @@ access to the `add_to_waitlist` function in the child module, so we mark the

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

```rust,ignore,does_not_compile
```rust,compile_fail
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-05/src/lib.rs}}
```

Expand Down
2 changes: 1 addition & 1 deletion src/ch08-01-vectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ scope. That rule applies in Listing 8-7, 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 if we also try to refer to that element later in the function:

```rust,ignore,does_not_compile
```rust,compile_fail
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-07/src/main.rs:here}}
```

Expand Down
4 changes: 2 additions & 2 deletions src/ch08-02-strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,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,does_not_compile
```rust,compile_fail
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-19/src/main.rs:here}}
```

Expand Down Expand Up @@ -262,7 +262,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,does_not_compile
```rust,compile_fail
let hello = "Здравствуйте";
let answer = &hello[0];
```
Expand Down
6 changes: 3 additions & 3 deletions src/ch09-02-recoverable-errors-with-result.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ us that the types don’t match. The error message will then tell us what the
type of `f` *is*. Let’s try it! We know that the return type of `File::open`
isn’t of type `u32`, so let’s change the `let f` statement to this:

```rust,ignore,does_not_compile
```rust,compile_fail
{{#rustdoc_include ../listings/ch09-error-handling/no-listing-02-ask-compiler-for-type/src/main.rs:here}}
```

Expand Down Expand Up @@ -398,7 +398,7 @@ compatible with this `return`.
In Listing 9-10, let’s look at the error we’ll get if we use the `?` operator
in a `main` function with a return type of `()`:

```rust,ignore,does_not_compile
```rust,compile_fail
{{#rustdoc_include ../listings/ch09-error-handling/listing-09-10/src/main.rs}}
```

Expand Down Expand Up @@ -484,7 +484,7 @@ code from Listing 9-10 but we’ve changed the return type of `main` to be
`Result<(), Box<dyn Error>>` and added a return value `Ok(())` to the end. This
code will now compile:

```rust,ignore
```rust,no_run
{{#rustdoc_include ../listings/ch09-error-handling/listing-09-12/src/main.rs}}
```

Expand Down
9 changes: 2 additions & 7 deletions src/ch09-03-to-panic-or-not-to-panic.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,8 @@ confidently use the values they receive. Listing 9-13 shows one way to define a
`Guess` type that will only create an instance of `Guess` if the `new` function
receives a value between 1 and 100.

<!-- Deliberately not using rustdoc_include here; the `main` function in the
file requires the `rand` crate. We do want to include it for reader
experimentation purposes, but don't want to include it for rustdoc testing
purposes. -->

```rust
{{#include ../listings/ch09-error-handling/listing-09-13/src/main.rs:here}}
```rust,ignore
{{#rustdoc_include ../listings/ch09-error-handling/listing-09-13/src/main.rs:here}}
```

<span class="caption">Listing 9-13: A `Guess` type that will only continue with
Expand Down
4 changes: 2 additions & 2 deletions src/ch10-01-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ compile yet, but we’ll fix it later in this chapter.

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

```rust,ignore,does_not_compile
```rust,compile_fail
{{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-05/src/main.rs}}
```

Expand Down Expand Up @@ -112,7 +112,7 @@ Listing 10-7, our code won’t compile.

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

```rust,ignore,does_not_compile
```rust,compile_fail
{{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-07/src/main.rs}}
```

Expand Down
6 changes: 3 additions & 3 deletions src/ch10-02-traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ the `summarize` method on its `item` parameter, which is of some type that
implements the `Summary` trait. To do this, we can use the `impl Trait`
syntax, like this:

```rust,ignore
```rust
{{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/no-listing-04-traits-as-parameters/src/lib.rs:here}}
```

Expand Down Expand Up @@ -301,7 +301,7 @@ bounds.
We can also use the `impl Trait` syntax in the return position to return a
value of some type that implements a trait, as shown here:

```rust,ignore
```rust
{{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/no-listing-05-returning-impl-trait/src/lib.rs:here}}
```

Expand All @@ -321,7 +321,7 @@ However, you can only use `impl Trait` if you’re returning a single type. For
example, this code that returns either a `NewsArticle` or a `Tweet` with the
return type specified as `impl Summary` wouldn’t work:

```rust,ignore,does_not_compile
```rust,compile_fail
{{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/no-listing-06-impl-trait-returns-one-type/src/lib.rs:here}}
```

Expand Down
10 changes: 5 additions & 5 deletions src/ch10-03-lifetime-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ program to reference data other than the data it’s intended to reference.
Consider the program in Listing 10-17, which has an outer scope and an inner
scope.

```rust,ignore,does_not_compile
```rust,compile_fail
{{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-17/src/main.rs:here}}
```

Expand Down Expand Up @@ -62,7 +62,7 @@ The Rust compiler has a *borrow checker* that compares scopes to determine
whether all borrows are valid. Listing 10-18 shows the same code as Listing
10-17 but with annotations showing the lifetimes of the variables.

```rust,ignore,does_not_compile
```rust,compile_fail
{{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-18/src/main.rs:here}}
```

Expand Down Expand Up @@ -122,7 +122,7 @@ won’t compile.

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

```rust,ignore,does_not_compile
```rust,compile_fail
{{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-21/src/main.rs:here}}
```

Expand Down Expand Up @@ -275,7 +275,7 @@ compile.

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

```rust,ignore,does_not_compile
```rust,compile_fail
{{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-24/src/main.rs:here}}
```

Expand Down Expand Up @@ -334,7 +334,7 @@ this attempted implementation of the `longest` function that won’t compile:

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

```rust,ignore,does_not_compile
```rust,compile_fail
{{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/no-listing-09-unrelated-lifetime/src/main.rs:here}}
```

Expand Down
2 changes: 1 addition & 1 deletion src/ch11-01-writing-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ value we got from the `greeting` function. Let’s change the test function,
giving it a custom failure message made from a format string with a placeholder
filled in with the actual value we got from the `greeting` function:

```rust,ignore
```rust,should_panic,ignore
{{#rustdoc_include ../listings/ch11-writing-automated-tests/no-listing-07-custom-failure-message/src/lib.rs:here}}
```

Expand Down
2 changes: 1 addition & 1 deletion src/ch12-03-improving-error-handling-and-modularity.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ next listing.

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

```rust,ignore
```rust,compile_fail
{{#rustdoc_include ../listings/ch12-an-io-project/listing-12-09/src/main.rs:here}}
```

Expand Down
Loading