Skip to content

Commit

Permalink
A solution for empty main in lib.rs, I think!
Browse files Browse the repository at this point in the history
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]: #1233 (comment)
  • Loading branch information
carols10cents committed Dec 9, 2020
1 parent 359895c commit eb60fed
Show file tree
Hide file tree
Showing 78 changed files with 77 additions and 234 deletions.
2 changes: 2 additions & 0 deletions ci/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ nonadministrators
nondeterministic
nonequality
nongeneric
noplayground
NotFound
nsprust
null's
Expand Down Expand Up @@ -470,6 +471,7 @@ supertraits
TcpListener
TcpStream
templating
test_harness
test's
TextField
That'd
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// ANCHOR: here
mod front_of_house {
mod hosting {
fn add_to_waitlist() {}
Expand All @@ -14,6 +13,3 @@ mod front_of_house {
fn take_payment() {}
}
}
// ANCHOR_END: here

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// ANCHOR: here
mod front_of_house {
pub mod hosting {
pub fn add_to_waitlist() {}
Expand All @@ -12,6 +11,3 @@ pub fn eat_at_restaurant() {
// Relative path
front_of_house::hosting::add_to_waitlist();
}
// ANCHOR_END: here

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// ANCHOR: here
fn serve_order() {}

mod back_of_house {
Expand All @@ -9,6 +8,3 @@ mod back_of_house {

fn cook_order() {}
}
// ANCHOR_END: here

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// ANCHOR: here
mod front_of_house {
pub mod hosting {
pub fn add_to_waitlist() {}
Expand All @@ -12,6 +11,3 @@ pub fn eat_at_restaurant() {
hosting::add_to_waitlist();
hosting::add_to_waitlist();
}
// ANCHOR_END: here

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// ANCHOR: here
mod front_of_house {
pub mod hosting {
pub fn add_to_waitlist() {}
Expand All @@ -12,6 +11,3 @@ pub fn eat_at_restaurant() {
hosting::add_to_waitlist();
hosting::add_to_waitlist();
}
// ANCHOR_END: here

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// ANCHOR: here
mod front_of_house {
pub mod hosting {
pub fn add_to_waitlist() {}
Expand All @@ -12,6 +11,3 @@ pub fn eat_at_restaurant() {
add_to_waitlist();
add_to_waitlist();
}
// ANCHOR_END: here

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// ANCHOR: here
mod front_of_house {
pub mod hosting {
pub fn add_to_waitlist() {}
Expand All @@ -12,6 +11,3 @@ pub fn eat_at_restaurant() {
hosting::add_to_waitlist();
hosting::add_to_waitlist();
}
// ANCHOR_END: here

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
// ANCHOR: here
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}
// ANCHOR_END: here

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,3 @@ mod tests {
}
}
// ANCHOR_END: here

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,3 @@ impl Rectangle {
}
}
// ANCHOR_END: here

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,3 @@ mod tests {
}
}
// ANCHOR_END: here

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// ANCHOR: here
pub fn add_two(a: i32) -> i32 {
a + 2
}
Expand All @@ -12,6 +11,3 @@ mod tests {
assert_eq!(4, add_two(2));
}
}
// ANCHOR_END: here

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// ANCHOR: here
pub struct Guess {
value: i32,
}
Expand All @@ -23,6 +22,3 @@ mod tests {
Guess::new(200);
}
}
// ANCHOR_END: here

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,3 @@ mod tests {
}
}
// ANCHOR_END: here

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
fn main() {}

// ANCHOR: here
fn prints_and_returns_10(a: i32) -> i32 {
println!("I got the value {}", a);
10
Expand All @@ -22,4 +19,3 @@ mod tests {
assert_eq!(5, value);
}
}
// ANCHOR_END: here
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// ANCHOR: here
pub fn add_two(a: i32) -> i32 {
internal_adder(a, 2)
}
Expand All @@ -16,6 +15,3 @@ mod tests {
assert_eq!(4, internal_adder(2, 2));
}
}
// ANCHOR_END: here

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
// ANCHOR: here
#[cfg(test)]
mod tests {
#[test]
fn exploration() {
assert_eq!(2 + 2, 4);
}
}
// ANCHOR_END: here

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,3 @@ mod tests {
}
}
// ANCHOR_END: here

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,3 @@ mod tests {
assert!(!smaller.can_hold(&larger));
}
}

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,3 @@ mod tests {
assert_eq!(4, add_two(2));
}
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// ANCHOR: here
pub fn greeting(name: &str) -> String {
format!("Hello {}!", name)
}
Expand All @@ -13,6 +12,3 @@ mod tests {
assert!(result.contains("Carol"));
}
}
// ANCHOR_END: here

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,3 @@ mod tests {
assert!(result.contains("Carol"));
}
}

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,3 @@ mod tests {
Guess::new(200);
}
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
#![allow(unused_variables)]
fn main() {}

// ANCHOR: here
#[cfg(test)]
mod tests {
#[test]
Expand All @@ -13,4 +9,3 @@ mod tests {
}
}
}
// ANCHOR_END: here
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// ANCHOR: here
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
Expand All @@ -9,6 +8,3 @@ fn it_works() {
fn expensive_test() {
// code that takes an hour to run
}
// ANCHOR_END: here

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,3 @@ fn expensive_test() {
// code that takes an hour to run
}
// ANCHOR_END: here

fn main() {}
2 changes: 0 additions & 2 deletions listings/ch12-an-io-project/listing-12-15/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,3 @@ Pick three.";
}
}
// ANCHOR_END: here

fn main() {}
2 changes: 0 additions & 2 deletions listings/ch12-an-io-project/listing-12-16/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,3 @@ Pick three.";
assert_eq!(vec!["safe, fast, productive."], search(query, contents));
}
}

fn main() {}
2 changes: 0 additions & 2 deletions listings/ch12-an-io-project/listing-12-20/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,3 @@ Trust me.";
}
}
// ANCHOR_END: here

fn main() {}
2 changes: 0 additions & 2 deletions listings/ch12-an-io-project/listing-12-21/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,3 @@ Trust me.";
);
}
}

fn main() {}
2 changes: 0 additions & 2 deletions listings/ch12-an-io-project/listing-12-22/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,3 @@ Trust me.";
);
}
}

fn main() {}
2 changes: 0 additions & 2 deletions listings/ch12-an-io-project/listing-12-23/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,3 @@ Trust me.";
);
}
}

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,3 @@ Pick three.";
assert_eq!(vec!["safe, fast, productive."], search(query, contents));
}
}

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,3 @@ Trust me.";
);
}
}

fn main() {}
2 changes: 0 additions & 2 deletions listings/ch13-functional-features/listing-13-15/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,3 @@ mod tests {
}
// ANCHOR_END: here
}

fn main() {}
2 changes: 0 additions & 2 deletions listings/ch13-functional-features/listing-13-16/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,3 @@ mod tests {
}
// ANCHOR_END: here
}

fn main() {}
2 changes: 0 additions & 2 deletions listings/ch13-functional-features/listing-13-19/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,3 @@ mod tests {
);
}
}

fn main() {}
2 changes: 0 additions & 2 deletions listings/ch13-functional-features/listing-13-20/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,3 @@ impl Counter {
Counter { count: 0 }
}
}

fn main() {}
2 changes: 0 additions & 2 deletions listings/ch13-functional-features/listing-13-21/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,3 @@ impl Iterator for Counter {
}
}
// ANCHOR_END: here

fn main() {}
2 changes: 0 additions & 2 deletions listings/ch13-functional-features/listing-13-22/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,3 @@ mod tests {
}
// ANCHOR_END: here
}

fn main() {}
2 changes: 0 additions & 2 deletions listings/ch13-functional-features/listing-13-23/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,3 @@ mod tests {
}
// ANCHOR_END: here
}

fn main() {}
2 changes: 0 additions & 2 deletions listings/ch13-functional-features/listing-13-27/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,3 @@ Trust me.";
);
}
}

fn main() {}
4 changes: 0 additions & 4 deletions listings/ch14-more-about-cargo/listing-14-03/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// ANCHOR: here
//! # Art
//!
//! A library for modeling artistic concepts.
Expand Down Expand Up @@ -31,6 +30,3 @@ pub mod utils {
// ANCHOR: here
}
}
// ANCHOR_END: here

fn main() {}
2 changes: 0 additions & 2 deletions listings/ch15-smart-pointers/listing-15-22/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,3 @@ mod tests {
}
}
// ANCHOR_END: here

fn main() {}
2 changes: 0 additions & 2 deletions listings/ch17-oop/listing-17-07/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,3 @@ impl Draw for Button {
}
}
// ANCHOR_END: here

fn main() {}
2 changes: 0 additions & 2 deletions listings/ch17-oop/listing-17-13/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,3 @@ trait State {}
struct Draft {}

impl State for Draft {}

fn main() {}
Loading

0 comments on commit eb60fed

Please sign in to comment.