Skip to content

Chapter 11.1 Listing 11-7 displayed code is incorrect #1206

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

Closed
ach619 opened this issue Mar 8, 2018 · 5 comments
Closed

Chapter 11.1 Listing 11-7 displayed code is incorrect #1206

ach619 opened this issue Mar 8, 2018 · 5 comments

Comments

@ach619
Copy link
Contributor

ach619 commented Mar 8, 2018

Example 11-7 is supposed to be a passing example but the listed code results in a failure to compile
Code listed is as follows:
#![allow(unused_variables)]
fn main() {
pub fn add_two(a: i32) -> i32 {
a + 2
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_adds_two() {
assert_eq!(4, add_two(2));
}
}
}

//Should result in:

running 1 test
test tests::it_adds_two ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

//Instead results in:

error[E0425]: cannot find function greeting in this scope
--> src/lib.rs:14:22
|
14 | let result = greeting("Carol");
| ^^^^^^^^ not found in this scope

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

warning: function is never used: main
--> src/lib.rs:3:1
|
3 | fn main() {
| ^^^^^^^^^
|
= note: #[warn(dead_code)] on by default

error: aborting due to previous error

error: Could not compile adder.
warning: build failed, waiting for other jobs to finish...
error: build failed

I can fix and submit if allowed. Thanks love the book

@ach619
Copy link
Contributor Author

ach619 commented Mar 8, 2018

The main() should be removed in the expanded code, it is unnecessary

@ach619
Copy link
Contributor Author

ach619 commented Mar 8, 2018

This fix should be applied to all further examples in Chapter 11.1 as all code is executed using cargo test in the src/lib.rs and does not require a main()

@carols10cents
Copy link
Member

Yup, you're right. Adding a hidden # fn main() {} should stop the automatic main wrapping. Example: https://github.com/rust-lang/book/blame/master/second-edition/src/ch15-06-reference-cycles.md#L24

We'd love a pull request! ❤️

@ach619
Copy link
Contributor Author

ach619 commented Mar 13, 2018

Ahh, I see. Took a look at the example in the link, thanks. making a pull request

@ach619
Copy link
Contributor Author

ach619 commented Mar 13, 2018

I submitted the pull request, I also did one for chapter 13.3 example 13-27 with the same issue. Thanks for letting me help.

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

2 participants