book
book copied to clipboard
ch11-03: Unnecessary import removal
Qualified call to add_two
is sufficient. There is no need to import adder
with use adder;
.
Articles
If our project is a binary crate that only contains a src/main.rs file and doesn’t have a src/lib.rs file, we can’t create integration tests in the tests directory and bring functions defined in the src/main.rs file into scope with a
use
statement. Only library crates expose functions that other crates can use; binary crates are meant to be run on their own.This is one of the reasons Rust projects that provide a binary have a straightforward src/main.rs file that calls logic that lives in the src/lib.rs file. Using that structure, integration tests can test the library crate with
use
to make the important functionality available. If the important functionality works, the small amount of code in the src/main.rs file will work as well, and that small amount of code doesn’t need to be tested.
debates using use
. While use
can be employed to import particular items, circumstances should be clearer. Mayhap listing 11-13 can be just changed to
use adder::add_two;
#[test]
fn it_adds_two() {
assert_eq!(4, add_two(2));
}
That would demonstrate scoping is taking its turn. I am not really sure what information removed article is supposed to bring in. Thus I cannot alter it.