Skip to content

Removes 'main{' and '}' from listing 11-12 so it compiles. #1339

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

Merged
merged 3 commits into from
May 3, 2018

Conversation

FabienTregan
Copy link
Contributor

Code snipet was not marked as " ```rust,ignore", then when copied it was surrounded with a "main {}" block which prevented it fom compiling.

@steveklabnik
Copy link
Member

Hm, sorry, I'm a bit confused. What is happening here? This shouldn't have any main in it at all?

@FabienTregan
Copy link
Contributor Author

Ok, I try to give a better description of what I wanted to solved with this commit :

-I checkout version b56c8f0
-I mdbook to generate html version of the book
-from /book/2018-edition/book/ch11-03-test-organization.html , I go to listing 11-12 and use the copy button
-when I past it, all the code is included (without added indentation) into a fn main {..}

#![allow(unused_variables)]
fn main() {
pub fn add_two(a: i32) -> i32 {
internal_adder(a, 2)
}

fn internal_adder(a: i32, b: i32) -> i32 {
a + b
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn internal() {
assert_eq!(4, internal_adder(2, 2));
}
}
}

This does compile (with rustc src/lib.rs in my dedicated 'learning rust' folder), but if I try cargo test (and this listing is supposed to show how to test private functions) I get :

error[E0425]: cannot find function internal_adder in this scope
--> src\lib.rs:20:23
|
20 | assert_eq!(4, internal_adder(2, 2));
| ^^^^^^^^^^^^^^ not found in this scope

warning: unused import: super::*
--> src\lib.rs:16:9
|
16 | use super::*;
| ^^^^^^^^
|
= note: #[warn(unused_imports)] on by default

error: aborting due to previous error

For more information about this error, try rustc --explain E0425.
error: Could not compile adder.

To learn more, run the command again with --verbose.`

Having a public function inside the main function seemed strange to me (I'm just learning rust). Also the fact that there is a missing indentation level inside main seemed suspect. I tryed on listing 11-13, saw that the main function was not added ; It apparently came from the added ,ignore in first line of listing 11-13 in the /book/2018-edition/src/ch11-03-test-organization.md file. I tryed adding ,ignore in listing 11-12 and it solved the problem, but I'm no longer sure it was the good solution to this problem.

Hope this helps,
Fabien.

@carols10cents
Copy link
Member

The playground integration and mdbook test auto wraps examples in main, a better solution than adding ignore is to add a comment with a fake main so that a wrapper main doesn't get added, see https://github.com/rust-lang/book/blame/master/second-edition/src/ch11-01-writing-tests.md#L51 for an example

@steveklabnik
Copy link
Member

I'm a bit surprised it does given the inclusion of #[test], but makes sense. Yes, I'd rather add an explicit main than ignore the test.

@FabienTregan
Copy link
Contributor Author

I updated my branch according to @carols10cents ' recommendation, thank you.

Copy link
Member

@steveklabnik steveklabnik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

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

Successfully merging this pull request may close these issues.

3 participants