Skip to content

Commit eb60fed

Browse files
committed
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]: https://github.com/rust-lang/mdBook/blob/d0deee90b04068ed949f524bb682a47fa26f2218/src/renderer/html_handlebars/hbs_renderer.rs#L805-L808 [undocumented]: rust-lang/rust#42288 (comment) [`test_harness`]: https://github.com/rust-lang/rust/blob/220352781c2585f0efb07ab0e758b136514de5b8/src/librustdoc/doctest.rs#L252 [stopped using]: rust-lang#1233 (comment)
1 parent 359895c commit eb60fed

File tree

78 files changed

+77
-234
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+77
-234
lines changed

ci/dictionary.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ nonadministrators
318318
nondeterministic
319319
nonequality
320320
nongeneric
321+
noplayground
321322
NotFound
322323
nsprust
323324
null's
@@ -470,6 +471,7 @@ supertraits
470471
TcpListener
471472
TcpStream
472473
templating
474+
test_harness
473475
test's
474476
TextField
475477
That'd

listings/ch07-managing-growing-projects/listing-07-01/src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// ANCHOR: here
21
mod front_of_house {
32
mod hosting {
43
fn add_to_waitlist() {}
@@ -14,6 +13,3 @@ mod front_of_house {
1413
fn take_payment() {}
1514
}
1615
}
17-
// ANCHOR_END: here
18-
19-
fn main() {}

listings/ch07-managing-growing-projects/listing-07-07/src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// ANCHOR: here
21
mod front_of_house {
32
pub mod hosting {
43
pub fn add_to_waitlist() {}
@@ -12,6 +11,3 @@ pub fn eat_at_restaurant() {
1211
// Relative path
1312
front_of_house::hosting::add_to_waitlist();
1413
}
15-
// ANCHOR_END: here
16-
17-
fn main() {}
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// ANCHOR: here
21
fn serve_order() {}
32

43
mod back_of_house {
@@ -9,6 +8,3 @@ mod back_of_house {
98

109
fn cook_order() {}
1110
}
12-
// ANCHOR_END: here
13-
14-
fn main() {}

listings/ch07-managing-growing-projects/listing-07-11/src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// ANCHOR: here
21
mod front_of_house {
32
pub mod hosting {
43
pub fn add_to_waitlist() {}
@@ -12,6 +11,3 @@ pub fn eat_at_restaurant() {
1211
hosting::add_to_waitlist();
1312
hosting::add_to_waitlist();
1413
}
15-
// ANCHOR_END: here
16-
17-
fn main() {}

listings/ch07-managing-growing-projects/listing-07-12/src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// ANCHOR: here
21
mod front_of_house {
32
pub mod hosting {
43
pub fn add_to_waitlist() {}
@@ -12,6 +11,3 @@ pub fn eat_at_restaurant() {
1211
hosting::add_to_waitlist();
1312
hosting::add_to_waitlist();
1413
}
15-
// ANCHOR_END: here
16-
17-
fn main() {}

listings/ch07-managing-growing-projects/listing-07-13/src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// ANCHOR: here
21
mod front_of_house {
32
pub mod hosting {
43
pub fn add_to_waitlist() {}
@@ -12,6 +11,3 @@ pub fn eat_at_restaurant() {
1211
add_to_waitlist();
1312
add_to_waitlist();
1413
}
15-
// ANCHOR_END: here
16-
17-
fn main() {}

listings/ch07-managing-growing-projects/listing-07-17/src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// ANCHOR: here
21
mod front_of_house {
32
pub mod hosting {
43
pub fn add_to_waitlist() {}
@@ -12,6 +11,3 @@ pub fn eat_at_restaurant() {
1211
hosting::add_to_waitlist();
1312
hosting::add_to_waitlist();
1413
}
15-
// ANCHOR_END: here
16-
17-
fn main() {}
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
// ANCHOR: here
21
#[cfg(test)]
32
mod tests {
43
#[test]
54
fn it_works() {
65
assert_eq!(2 + 2, 4);
76
}
87
}
9-
// ANCHOR_END: here
10-
11-
fn main() {}

listings/ch11-writing-automated-tests/listing-11-03/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,3 @@ mod tests {
1212
}
1313
}
1414
// ANCHOR_END: here
15-
16-
fn main() {}

0 commit comments

Comments
 (0)