mirror of https://github.com/rust-lang/book
A solution for empty main in lib.rs, I think!
The reasons we needed empty `fn main() {}`s were twofold: - Avoid confusing people when they click the "expand" button on the code listing and see the auto-main wrapping - Avoid failing doctests when running `mdbook test` that don't work when rustdoc wraps a code listing in main I think I have a solution that mostly solves these cases. I don't know why this didn't occur to me before. Here's my current thinking in case these assumptions turn out to be wrong: There are a [few things that tell mdbook to disable the main-wrapping][mdbook], and I hadn't noticed one of them until now: if you annotate a code block with `noplayground`, it won't add a `main` around it (and it also won't have the "play" button in the upper right that runs the block and inserts the result into the page). So instead of putting an empty `fn main() {}` at the bottom of src/lib.rs files that doesn't make sense, annotate those listings with `noplayground`. I don't think anyone will miss the play button anyway because: - The play button doesn't run tests, so there wasn't any output for these examples anyway - If an example doesn't compile, we have it marked `ignore` so that it doesn't make the tests fail, and `ignore` also disables the play button, so there isn't a way to see compiler errors either In most of these cases, `mdbook test` that runs these as doctests will still wrap these in main, but the tests still pass. There are some cases, mostly around modules and using `crate::` that won't pass as doctests when wrapped in main. For those, I've annotated them with the [undocumented][] [`test_harness`][] attribute that apparently I was using at some point and then [stopped using][] and now I've decided to use again, but maybe send in a PR to rust-lang/rust to change the name to `no_main` and document it or something. In any case, that shouldn't affect readers at all. [mdbook]:d0deee90b0/src/renderer/html_handlebars/hbs_renderer.rs (L805-L808)
[undocumented]: https://github.com/rust-lang/rust/issues/42288#issuecomment-309661382 [`test_harness`]:220352781c/src/librustdoc/doctest.rs (L252)
[stopped using]: https://github.com/rust-lang/book/pull/1233#discussion_r175515585
This commit is contained in:
parent
359895c6b2
commit
eb60fedc9c
|
@ -318,6 +318,7 @@ nonadministrators
|
|||
nondeterministic
|
||||
nonequality
|
||||
nongeneric
|
||||
noplayground
|
||||
NotFound
|
||||
nsprust
|
||||
null's
|
||||
|
@ -470,6 +471,7 @@ supertraits
|
|||
TcpListener
|
||||
TcpStream
|
||||
templating
|
||||
test_harness
|
||||
test's
|
||||
TextField
|
||||
That'd
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
// ANCHOR: here
|
||||
mod front_of_house {
|
||||
mod hosting {
|
||||
fn add_to_waitlist() {}
|
||||
|
@ -14,6 +13,3 @@ mod front_of_house {
|
|||
fn take_payment() {}
|
||||
}
|
||||
}
|
||||
// ANCHOR_END: here
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
// ANCHOR: here
|
||||
mod front_of_house {
|
||||
pub mod hosting {
|
||||
pub fn add_to_waitlist() {}
|
||||
|
@ -12,6 +11,3 @@ pub fn eat_at_restaurant() {
|
|||
// Relative path
|
||||
front_of_house::hosting::add_to_waitlist();
|
||||
}
|
||||
// ANCHOR_END: here
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
// ANCHOR: here
|
||||
fn serve_order() {}
|
||||
|
||||
mod back_of_house {
|
||||
|
@ -9,6 +8,3 @@ mod back_of_house {
|
|||
|
||||
fn cook_order() {}
|
||||
}
|
||||
// ANCHOR_END: here
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
// ANCHOR: here
|
||||
mod front_of_house {
|
||||
pub mod hosting {
|
||||
pub fn add_to_waitlist() {}
|
||||
|
@ -12,6 +11,3 @@ pub fn eat_at_restaurant() {
|
|||
hosting::add_to_waitlist();
|
||||
hosting::add_to_waitlist();
|
||||
}
|
||||
// ANCHOR_END: here
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
// ANCHOR: here
|
||||
mod front_of_house {
|
||||
pub mod hosting {
|
||||
pub fn add_to_waitlist() {}
|
||||
|
@ -12,6 +11,3 @@ pub fn eat_at_restaurant() {
|
|||
hosting::add_to_waitlist();
|
||||
hosting::add_to_waitlist();
|
||||
}
|
||||
// ANCHOR_END: here
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
// ANCHOR: here
|
||||
mod front_of_house {
|
||||
pub mod hosting {
|
||||
pub fn add_to_waitlist() {}
|
||||
|
@ -12,6 +11,3 @@ pub fn eat_at_restaurant() {
|
|||
add_to_waitlist();
|
||||
add_to_waitlist();
|
||||
}
|
||||
// ANCHOR_END: here
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
// ANCHOR: here
|
||||
mod front_of_house {
|
||||
pub mod hosting {
|
||||
pub fn add_to_waitlist() {}
|
||||
|
@ -12,6 +11,3 @@ pub fn eat_at_restaurant() {
|
|||
hosting::add_to_waitlist();
|
||||
hosting::add_to_waitlist();
|
||||
}
|
||||
// ANCHOR_END: here
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
// ANCHOR: here
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
|
@ -6,6 +5,3 @@ mod tests {
|
|||
assert_eq!(2 + 2, 4);
|
||||
}
|
||||
}
|
||||
// ANCHOR_END: here
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -12,5 +12,3 @@ mod tests {
|
|||
}
|
||||
}
|
||||
// ANCHOR_END: here
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -11,5 +11,3 @@ impl Rectangle {
|
|||
}
|
||||
}
|
||||
// ANCHOR_END: here
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -30,5 +30,3 @@ mod tests {
|
|||
}
|
||||
}
|
||||
// ANCHOR_END: here
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
// ANCHOR: here
|
||||
pub fn add_two(a: i32) -> i32 {
|
||||
a + 2
|
||||
}
|
||||
|
@ -12,6 +11,3 @@ mod tests {
|
|||
assert_eq!(4, add_two(2));
|
||||
}
|
||||
}
|
||||
// ANCHOR_END: here
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
// ANCHOR: here
|
||||
pub struct Guess {
|
||||
value: i32,
|
||||
}
|
||||
|
@ -23,6 +22,3 @@ mod tests {
|
|||
Guess::new(200);
|
||||
}
|
||||
}
|
||||
// ANCHOR_END: here
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -33,5 +33,3 @@ mod tests {
|
|||
}
|
||||
}
|
||||
// ANCHOR_END: here
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -1,6 +1,3 @@
|
|||
fn main() {}
|
||||
|
||||
// ANCHOR: here
|
||||
fn prints_and_returns_10(a: i32) -> i32 {
|
||||
println!("I got the value {}", a);
|
||||
10
|
||||
|
@ -22,4 +19,3 @@ mod tests {
|
|||
assert_eq!(5, value);
|
||||
}
|
||||
}
|
||||
// ANCHOR_END: here
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
// ANCHOR: here
|
||||
pub fn add_two(a: i32) -> i32 {
|
||||
internal_adder(a, 2)
|
||||
}
|
||||
|
@ -16,6 +15,3 @@ mod tests {
|
|||
assert_eq!(4, internal_adder(2, 2));
|
||||
}
|
||||
}
|
||||
// ANCHOR_END: here
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
// ANCHOR: here
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
|
@ -6,6 +5,3 @@ mod tests {
|
|||
assert_eq!(2 + 2, 4);
|
||||
}
|
||||
}
|
||||
// ANCHOR_END: here
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -47,5 +47,3 @@ mod tests {
|
|||
}
|
||||
}
|
||||
// ANCHOR_END: here
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -45,5 +45,3 @@ mod tests {
|
|||
assert!(!smaller.can_hold(&larger));
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -13,5 +13,3 @@ mod tests {
|
|||
assert_eq!(4, add_two(2));
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
// ANCHOR: here
|
||||
pub fn greeting(name: &str) -> String {
|
||||
format!("Hello {}!", name)
|
||||
}
|
||||
|
@ -13,6 +12,3 @@ mod tests {
|
|||
assert!(result.contains("Carol"));
|
||||
}
|
||||
}
|
||||
// ANCHOR_END: here
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -14,5 +14,3 @@ mod tests {
|
|||
assert!(result.contains("Carol"));
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -25,5 +25,3 @@ mod tests {
|
|||
Guess::new(200);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -1,7 +1,3 @@
|
|||
#![allow(unused_variables)]
|
||||
fn main() {}
|
||||
|
||||
// ANCHOR: here
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
|
@ -13,4 +9,3 @@ mod tests {
|
|||
}
|
||||
}
|
||||
}
|
||||
// ANCHOR_END: here
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
// ANCHOR: here
|
||||
#[test]
|
||||
fn it_works() {
|
||||
assert_eq!(2 + 2, 4);
|
||||
|
@ -9,6 +8,3 @@ fn it_works() {
|
|||
fn expensive_test() {
|
||||
// code that takes an hour to run
|
||||
}
|
||||
// ANCHOR_END: here
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -10,5 +10,3 @@ fn expensive_test() {
|
|||
// code that takes an hour to run
|
||||
}
|
||||
// ANCHOR_END: here
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -42,5 +42,3 @@ Pick three.";
|
|||
}
|
||||
}
|
||||
// ANCHOR_END: here
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -46,5 +46,3 @@ Pick three.";
|
|||
assert_eq!(vec!["safe, fast, productive."], search(query, contents));
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -74,5 +74,3 @@ Trust me.";
|
|||
}
|
||||
}
|
||||
// ANCHOR_END: here
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -90,5 +90,3 @@ Trust me.";
|
|||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -99,5 +99,3 @@ Trust me.";
|
|||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -108,5 +108,3 @@ Trust me.";
|
|||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -46,5 +46,3 @@ Pick three.";
|
|||
assert_eq!(vec!["safe, fast, productive."], search(query, contents));
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -104,5 +104,3 @@ Trust me.";
|
|||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -14,5 +14,3 @@ mod tests {
|
|||
}
|
||||
// ANCHOR_END: here
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -13,5 +13,3 @@ mod tests {
|
|||
}
|
||||
// ANCHOR_END: here
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -46,5 +46,3 @@ mod tests {
|
|||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -7,5 +7,3 @@ impl Counter {
|
|||
Counter { count: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -22,5 +22,3 @@ impl Iterator for Counter {
|
|||
}
|
||||
}
|
||||
// ANCHOR_END: here
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -39,5 +39,3 @@ mod tests {
|
|||
}
|
||||
// ANCHOR_END: here
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -49,5 +49,3 @@ mod tests {
|
|||
}
|
||||
// ANCHOR_END: here
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -109,5 +109,3 @@ Trust me.";
|
|||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
// ANCHOR: here
|
||||
//! # Art
|
||||
//!
|
||||
//! A library for modeling artistic concepts.
|
||||
|
@ -31,6 +30,3 @@ pub mod utils {
|
|||
// ANCHOR: here
|
||||
}
|
||||
}
|
||||
// ANCHOR_END: here
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -76,5 +76,3 @@ mod tests {
|
|||
}
|
||||
}
|
||||
// ANCHOR_END: here
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -27,5 +27,3 @@ impl Draw for Button {
|
|||
}
|
||||
}
|
||||
// ANCHOR_END: here
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -26,5 +26,3 @@ trait State {}
|
|||
struct Draft {}
|
||||
|
||||
impl State for Draft {}
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -30,5 +30,3 @@ trait State {}
|
|||
struct Draft {}
|
||||
|
||||
impl State for Draft {}
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -50,5 +50,3 @@ impl State for PendingReview {
|
|||
}
|
||||
}
|
||||
// ANCHOR_END: here
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -23,5 +23,3 @@ impl Iterator for Counter {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -13,5 +13,3 @@ pub trait Write {
|
|||
fn write_fmt(&mut self, fmt: fmt::Arguments) -> Result<()>;
|
||||
}
|
||||
// ANCHOR_END: there
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -6,5 +6,3 @@ fn bar() -> ! {
|
|||
// ANCHOR: here
|
||||
}
|
||||
// ANCHOR_END: here
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -26,5 +26,3 @@ impl ThreadPool {
|
|||
// ANCHOR: here
|
||||
}
|
||||
// ANCHOR_END: here
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -51,5 +51,3 @@ impl Worker {
|
|||
}
|
||||
}
|
||||
// ANCHOR_END: here
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -58,5 +58,3 @@ impl Worker {
|
|||
Worker { id, thread }
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -74,5 +74,3 @@ impl Worker {
|
|||
}
|
||||
}
|
||||
// ANCHOR_END: here
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -67,5 +67,3 @@ impl Worker {
|
|||
Worker { id, thread }
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
// ANCHOR: here
|
||||
use std::sync::mpsc;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
|
@ -100,6 +99,3 @@ impl Worker {
|
|||
}
|
||||
}
|
||||
}
|
||||
// ANCHOR_END: here
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
// ANCHOR: here
|
||||
pub struct ThreadPool;
|
||||
|
||||
impl ThreadPool {
|
||||
|
@ -6,6 +5,3 @@ impl ThreadPool {
|
|||
ThreadPool
|
||||
}
|
||||
}
|
||||
// ANCHOR_END: here
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -16,5 +16,3 @@ impl ThreadPool {
|
|||
}
|
||||
}
|
||||
// ANCHOR_END: here
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -84,5 +84,3 @@ impl Worker {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -29,8 +29,8 @@ Listing 7-1 into *src/lib.rs* to define some modules and function signatures.
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-01/src/lib.rs:here}}
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-01/src/lib.rs}}
|
||||
```
|
||||
|
||||
<span class="caption">Listing 7-1: A `front_of_house` module containing other
|
||||
|
|
|
@ -141,8 +141,8 @@ keyword before its definition, as in Listing 7-7.
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-07/src/lib.rs:here}}
|
||||
```rust,noplayground,test_harness
|
||||
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-07/src/lib.rs}}
|
||||
```
|
||||
|
||||
<span class="caption">Listing 7-7: Adding the `pub` keyword to `mod hosting`
|
||||
|
@ -184,8 +184,8 @@ the path to `serve_order` starting with `super`:
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-08/src/lib.rs:here}}
|
||||
```rust,noplayground,test_harness
|
||||
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-08/src/lib.rs}}
|
||||
```
|
||||
|
||||
<span class="caption">Listing 7-8: Calling a function using a relative path
|
||||
|
|
|
@ -15,8 +15,8 @@ scope of the `eat_at_restaurant` function so we only have to specify
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-11/src/lib.rs:here}}
|
||||
```rust,noplayground,test_harness
|
||||
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-11/src/lib.rs}}
|
||||
```
|
||||
|
||||
<span class="caption">Listing 7-11: Bringing a module into scope with
|
||||
|
@ -34,8 +34,8 @@ Listing 7-11.
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-12/src/lib.rs:here}}
|
||||
```rust,noplayground,test_harness
|
||||
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-12/src/lib.rs}}
|
||||
```
|
||||
|
||||
<span class="caption">Listing 7-12: Bringing a module into scope with `use` and
|
||||
|
@ -50,8 +50,8 @@ the `add_to_waitlist` function to achieve the same result, as in Listing 7-13.
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-13/src/lib.rs:here}}
|
||||
```rust,noplayground,test_harness
|
||||
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-13/src/lib.rs}}
|
||||
```
|
||||
|
||||
<span class="caption">Listing 7-13: Bringing the `add_to_waitlist` function
|
||||
|
@ -135,8 +135,8 @@ changed to `pub use`.
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-17/src/lib.rs:here}}
|
||||
```rust,noplayground,test_harness
|
||||
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-17/src/lib.rs}}
|
||||
```
|
||||
|
||||
<span class="caption">Listing 7-17: Making a name available for any code to use
|
||||
|
|
|
@ -46,8 +46,8 @@ Listing 11-1.
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
{{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-01/src/lib.rs:here}}
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-01/src/lib.rs}}
|
||||
```
|
||||
|
||||
<span class="caption">Listing 11-1: The test module and function generated
|
||||
|
@ -107,8 +107,8 @@ so:
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
{{#rustdoc_include ../listings/ch11-writing-automated-tests/no-listing-01-changing-test-name/src/lib.rs:here}}
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch11-writing-automated-tests/no-listing-01-changing-test-name/src/lib.rs}}
|
||||
```
|
||||
|
||||
Then run `cargo test` again. The output now shows `exploration` instead of
|
||||
|
@ -127,7 +127,7 @@ which is to call the `panic!` macro. Enter the new test, `another`, so your
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust,panics
|
||||
```rust,panics,noplayground
|
||||
{{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-03/src/lib.rs:here}}
|
||||
```
|
||||
|
||||
|
@ -177,7 +177,7 @@ method, which are repeated here in Listing 11-5. Let’s put this code in the
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-05/src/lib.rs:here}}
|
||||
```
|
||||
|
||||
|
@ -192,7 +192,7 @@ has a width of 5 and a height of 1.
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-06/src/lib.rs:here}}
|
||||
```
|
||||
|
||||
|
@ -222,7 +222,7 @@ rectangle cannot hold a larger rectangle:
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch11-writing-automated-tests/no-listing-02-adding-another-rectangle-test/src/lib.rs:here}}
|
||||
```
|
||||
|
||||
|
@ -239,7 +239,7 @@ introduce a bug in our code. Let’s change the implementation of the `can_hold`
|
|||
method by replacing the greater than sign with a less than sign when it
|
||||
compares the widths:
|
||||
|
||||
```rust,not_desired_behavior
|
||||
```rust,not_desired_behavior,noplayground
|
||||
{{#rustdoc_include ../listings/ch11-writing-automated-tests/no-listing-03-introducing-a-bug/src/lib.rs:here}}
|
||||
```
|
||||
|
||||
|
@ -272,8 +272,8 @@ parameter and returns the result. Then we test this function using the
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
{{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-07/src/lib.rs:here}}
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-07/src/lib.rs}}
|
||||
```
|
||||
|
||||
<span class="caption">Listing 11-7: Testing the function `add_two` using the
|
||||
|
@ -293,7 +293,7 @@ Let’s introduce a bug into our code to see what it looks like when a test that
|
|||
uses `assert_eq!` fails. Change the implementation of the `add_two` function to
|
||||
instead add `3`:
|
||||
|
||||
```rust,not_desired_behavior
|
||||
```rust,not_desired_behavior,noplayground
|
||||
{{#rustdoc_include ../listings/ch11-writing-automated-tests/no-listing-04-bug-in-add-two/src/lib.rs:here}}
|
||||
```
|
||||
|
||||
|
@ -358,8 +358,8 @@ want to test that the name we pass into the function appears in the output:
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
{{#rustdoc_include ../listings/ch11-writing-automated-tests/no-listing-05-greeter/src/lib.rs:here}}
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch11-writing-automated-tests/no-listing-05-greeter/src/lib.rs}}
|
||||
```
|
||||
|
||||
The requirements for this program haven’t been agreed upon yet, and we’re
|
||||
|
@ -372,7 +372,7 @@ input parameter.
|
|||
Let’s introduce a bug into this code by changing `greeting` to not include
|
||||
`name` to see what this test failure looks like:
|
||||
|
||||
```rust,not_desired_behavior
|
||||
```rust,not_desired_behavior,noplayground
|
||||
{{#rustdoc_include ../listings/ch11-writing-automated-tests/no-listing-06-greeter-with-bug/src/lib.rs:here}}
|
||||
```
|
||||
|
||||
|
@ -420,8 +420,8 @@ happen when we expect them to.
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
{{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-08/src/lib.rs:here}}
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-08/src/lib.rs}}
|
||||
```
|
||||
|
||||
<span class="caption">Listing 11-8: Testing that a condition will cause a
|
||||
|
@ -438,7 +438,7 @@ passes:
|
|||
Looks good! Now let’s introduce a bug in our code by removing the condition
|
||||
that the `new` function will panic if the value is greater than 100:
|
||||
|
||||
```rust,not_desired_behavior
|
||||
```rust,not_desired_behavior,noplayground
|
||||
{{#rustdoc_include ../listings/ch11-writing-automated-tests/no-listing-08-guess-with-bug/src/lib.rs:here}}
|
||||
```
|
||||
|
||||
|
@ -463,7 +463,7 @@ different messages depending on whether the value is too small or too large.
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-09/src/lib.rs:here}}
|
||||
```
|
||||
|
||||
|
@ -506,8 +506,8 @@ So far, we’ve written tests that panic when they fail. We can also write tests
|
|||
that use `Result<T, E>`! Here’s the test from Listing 11-1, rewritten to use
|
||||
`Result<T, E>` and return an `Err` instead of panicking:
|
||||
|
||||
```rust
|
||||
{{#rustdoc_include ../listings/ch11-writing-automated-tests/no-listing-10-result-in-tests/src/lib.rs:here}}
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch11-writing-automated-tests/no-listing-10-result-in-tests/src/lib.rs}}
|
||||
```
|
||||
|
||||
The `it_works` function now has a return type, `Result<(), String>`. In the
|
||||
|
|
|
@ -61,8 +61,8 @@ parameter and returns 10, as well as a test that passes and a test that fails.
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust,panics
|
||||
{{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-10/src/lib.rs:here}}
|
||||
```rust,panics,noplayground
|
||||
{{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-10/src/lib.rs}}
|
||||
```
|
||||
|
||||
<span class="caption">Listing 11-10: Tests for a function that calls
|
||||
|
@ -159,8 +159,8 @@ here:
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
{{#rustdoc_include ../listings/ch11-writing-automated-tests/no-listing-11-ignore-a-test/src/lib.rs:here}}
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch11-writing-automated-tests/no-listing-11-ignore-a-test/src/lib.rs}}
|
||||
```
|
||||
|
||||
After `#[test]` we add the `#[ignore]` line to the test we want to exclude. Now
|
||||
|
|
|
@ -60,8 +60,8 @@ Consider the code in Listing 11-12 with the private function `internal_adder`.
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
{{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-12/src/lib.rs:here}}
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-12/src/lib.rs}}
|
||||
```
|
||||
|
||||
<span class="caption">Listing 11-12: Testing a private function</span>
|
||||
|
|
|
@ -60,7 +60,7 @@ containing the line `"safe, fast, productive."`
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch12-an-io-project/listing-12-16/src/lib.rs:here}}
|
||||
```
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@ tests, as shown in Listing 12-20.
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch12-an-io-project/listing-12-20/src/lib.rs:here}}
|
||||
```
|
||||
|
||||
|
@ -50,7 +50,7 @@ they’ll be the same case when we check whether the line contains the query.
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch12-an-io-project/listing-12-21/src/lib.rs:here}}
|
||||
```
|
||||
|
||||
|
@ -120,7 +120,7 @@ in Listing 12-23.
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch12-an-io-project/listing-12-23/src/lib.rs:here}}
|
||||
```
|
||||
|
||||
|
|
|
@ -77,7 +77,7 @@ from the vector.
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch13-functional-features/listing-13-15/src/lib.rs:here}}
|
||||
```
|
||||
|
||||
|
@ -116,7 +116,7 @@ test illustrating a use of the `sum` method:
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch13-functional-features/listing-13-16/src/lib.rs:here}}
|
||||
```
|
||||
|
||||
|
@ -196,7 +196,7 @@ instances. It will return only shoes that are the specified size.
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch13-functional-features/listing-13-19/src/lib.rs}}
|
||||
```
|
||||
|
||||
|
@ -241,7 +241,7 @@ Listing 13-20 has the definition of the `Counter` struct and an associated
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch13-functional-features/listing-13-20/src/lib.rs}}
|
||||
```
|
||||
|
||||
|
@ -261,8 +261,8 @@ iterator is used, as shown in Listing 13-21:
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
{{#rustdoc_include ../listings/ch13-functional-features/listing-13-21/src/lib.rs:here}}
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch13-functional-features/listing-13-21/src/lib.rs}}
|
||||
```
|
||||
|
||||
<span class="caption">Listing 13-21: Implementing the `Iterator` trait on our
|
||||
|
@ -287,7 +287,7 @@ with the iterator created from a vector in Listing 13-15.
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch13-functional-features/listing-13-22/src/lib.rs:here}}
|
||||
```
|
||||
|
||||
|
@ -312,7 +312,7 @@ together, we could do so, as shown in the test in Listing 13-23:
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch13-functional-features/listing-13-23/src/lib.rs:here}}
|
||||
```
|
||||
|
||||
|
|
|
@ -98,7 +98,7 @@ Listing 12-23 to use the `next` method:
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch13-functional-features/listing-13-27/src/lib.rs:here}}
|
||||
```
|
||||
|
||||
|
|
|
@ -176,8 +176,8 @@ function named `mix`, as shown in Listing 14-3:
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
{{#rustdoc_include ../listings/ch14-more-about-cargo/listing-14-03/src/lib.rs:here}}
|
||||
```rust,noplayground,test_harness
|
||||
{{#rustdoc_include ../listings/ch14-more-about-cargo/listing-14-03/src/lib.rs}}
|
||||
```
|
||||
|
||||
<span class="caption">Listing 14-3: An `art` library with items organized into
|
||||
|
|
|
@ -195,7 +195,7 @@ shows what that looks like:
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch15-smart-pointers/listing-15-22/src/lib.rs:here}}
|
||||
```
|
||||
|
||||
|
|
|
@ -138,7 +138,7 @@ might have fields for `width`, `height`, and `label`, as shown in Listing 17-7:
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch17-oop/listing-17-07/src/lib.rs:here}}
|
||||
```
|
||||
|
||||
|
|
|
@ -107,7 +107,7 @@ Post` block:
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch17-oop/listing-17-13/src/lib.rs:here}}
|
||||
```
|
||||
|
||||
|
@ -135,7 +135,7 @@ be empty. Listing 17-14 shows this placeholder implementation:
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch17-oop/listing-17-14/src/lib.rs:here}}
|
||||
```
|
||||
|
||||
|
@ -152,7 +152,7 @@ change its state from `Draft` to `PendingReview`. Listing 17-15 shows this code:
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch17-oop/listing-17-15/src/lib.rs:here}}
|
||||
```
|
||||
|
||||
|
|
|
@ -108,7 +108,7 @@ the `Write` trait:
|
|||
The `Result<..., Error>` is repeated a lot. As such, `std::io` has this type of
|
||||
alias declaration:
|
||||
|
||||
```rust
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch19-advanced-features/no-listing-06-result-alias/src/lib.rs:here}}
|
||||
```
|
||||
|
||||
|
@ -117,7 +117,7 @@ qualified alias `std::io::Result<T>`—that is, a `Result<T, E>` with the `E`
|
|||
filled in as `std::io::Error`. The `Write` trait function signatures end up
|
||||
looking like this:
|
||||
|
||||
```rust
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch19-advanced-features/no-listing-06-result-alias/src/lib.rs:there}}
|
||||
```
|
||||
|
||||
|
@ -133,7 +133,7 @@ Rust has a special type named `!` that’s known in type theory lingo as the
|
|||
because it stands in the place of the return type when a function will never
|
||||
return. Here is an example:
|
||||
|
||||
```rust
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch19-advanced-features/no-listing-07-never-type/src/lib.rs:here}}
|
||||
```
|
||||
|
||||
|
|
|
@ -189,8 +189,8 @@ characteristics:
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
{{#rustdoc_include ../listings/ch20-web-server/no-listing-02-impl-threadpool-new/src/lib.rs:here}}
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch20-web-server/no-listing-02-impl-threadpool-new/src/lib.rs}}
|
||||
```
|
||||
|
||||
We chose `usize` as the type of the `size` parameter, because we know that a
|
||||
|
@ -246,7 +246,7 @@ the thread will take to execute. Let’s create an `execute` method on
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch20-web-server/no-listing-03-define-execute/src/lib.rs:here}}
|
||||
```
|
||||
|
||||
|
@ -287,7 +287,7 @@ zero by using the `assert!` macro, as shown in Listing 20-13.
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch20-web-server/listing-20-13/src/lib.rs:here}}
|
||||
```
|
||||
|
||||
|
@ -404,7 +404,7 @@ Ready? Here is Listing 20-15 with one way to make the preceding modifications.
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch20-web-server/listing-20-15/src/lib.rs:here}}
|
||||
```
|
||||
|
||||
|
@ -459,7 +459,7 @@ the channel.
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch20-web-server/listing-20-16/src/lib.rs:here}}
|
||||
```
|
||||
|
||||
|
@ -511,7 +511,7 @@ receiver at a time. Listing 20-18 shows the changes we need to make.
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch20-web-server/listing-20-18/src/lib.rs:here}}
|
||||
```
|
||||
|
||||
|
@ -535,7 +535,7 @@ at Listing 20-19.
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch20-web-server/listing-20-19/src/lib.rs:here}}
|
||||
```
|
||||
|
||||
|
|
|
@ -112,7 +112,7 @@ variants.
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch20-web-server/no-listing-07-define-message-enum/src/lib.rs:here}}
|
||||
```
|
||||
|
||||
|
@ -261,8 +261,8 @@ Here’s the full code for reference:
|
|||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust
|
||||
{{#rustdoc_include ../listings/ch20-web-server/listing-20-25/src/lib.rs:here}}
|
||||
```rust,noplayground
|
||||
{{#rustdoc_include ../listings/ch20-web-server/listing-20-25/src/lib.rs}}
|
||||
```
|
||||
|
||||
We could do more here! If you want to continue enhancing this project, here are
|
||||
|
|
Loading…
Reference in New Issue