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

Wrapping code in fn main too much in the book's code examples #31249

Closed
carols10cents opened this issue Jan 28, 2016 · 4 comments
Closed

Wrapping code in fn main too much in the book's code examples #31249

carols10cents opened this issue Jan 28, 2016 · 4 comments

Comments

@carols10cents
Copy link
Member

I've only found this to be a problem in the testing section but there could be more.

In the code example that follows "Let's make our test fail:", if you click the "Run" button to open that example in the playground, running it exits successfully when it's supposed to fail because the test gets wrapped in fn main:

screen shot 2016-01-27 at 6 53 20 pm

I'm not sure exactly where the fn main is being added, but if I inspect the HTML of the page there's a <span class="rusttest"> that contains fn main:

screen shot 2016-01-27 at 6 52 39 pm

Is there a way to opt out of the main wrapping in this case?

I know @steveklabnik is redoing the book; is integration with play.rust-lang.org going to be a part of the new book too, or is this irrelevant? Is this something that's just part of TRPL or is it part of rustbook that other books might use?

I'm going to go research the answers to these questions but wanted to post this in the meantime :)

@tomjakubowski
Copy link
Contributor

This may not be the "right" way, but…

rustdoc has a heuristic for guessing whether an example provides its own fn main() { }. The heuristic is: "does the example contain the string "fn main". If rutsdoc thinks the example provides its own fn main() { }, then it won't wrap the example in a main function. So maybe you can try writing a doc comment like this:

//! blah blah blah, example:
//!
//! ```
//! # // fn main
//! #[test]
//! fn blah() {
//!     assert!(false);
//! }
//! ```

I don't have a copy of rustbook handy to test this with, unfortunately.

@tomjakubowski
Copy link
Contributor

Here's where that heuristic is documented: https://doc.rust-lang.org/book/documentation.html#documentation-as-tests

And, if you don't know, starting a line in an example off with # will hide that line when rustdoc renders the documentation.

@steveklabnik
Copy link
Member

My email got lost in the GitHub outage :) Re-written below:


This is just normal Rustdoc behavior for examples. We can either:

Add a new rule about #[test]s

Fix it online by writing a main() explicitly.

The second is a lot less work, and isn't really worse. There's already a few situations where the default doesn't work and you have to do this; it's not clear to me more complex rules is helpful.

@carols10cents
Copy link
Member Author

I've been saving the tab with half a response started in this textarea since the outage ;)

I researched what the new book is doing, and saw that it was using mdBook.

mdBook has the code-running feature implemented in a different way-- described in https://github.com/azerupi/mdBook/issues/29 (example. It also lets you hide lines with comments! So the new book might have a similar problem, but the code causing it is not the same.

So yeah, I guess this spot just needs a # // fn main() {} :)

steveklabnik added a commit to steveklabnik/rust that referenced this issue Jan 29, 2016
Rustdoc will automatically wrap things in main, but this doesn't work
here.

Fixes rust-lang#31249
Manishearth added a commit to Manishearth/rust that referenced this issue Jan 30, 2016
Rustdoc will automatically wrap things in main, but this doesn't work
here.

Fixes rust-lang#31249
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants