The src/lib.rs can be used as a module in project.
1 2 3 4 5 6 7 8
// src/main.rs mod lib;
use lib::hello;
fnmain() { println!("{}", hello("main")); }
You can get expected result by running with play, the package name in this example.
1 2 3 4
❯ cargo run --bin play Finished dev [unoptimized + debuginfo] target(s) in 0.00s Running `target/debug/play` hello, main
Bin
It is easy to create other executables by adding them in directory src/bin. Cargo will find all executables in it. So it is unnecessary to config Cargo.toml like that.
But there are some difference from src/main.rs that we cannot find module named lib in the scope of src/bin/another_executable.rs.
1 2 3 4 5 6 7 8
// src/bin/another_executable.rs mod lib; // not work!
❯ cargo check Checking play v0.1.0 (/Users/jtr109/mystuff/learn/rust/play) error[E0583]: file not found for module `lib` --> src/bin/another_executable.rs:5:5 | 5 | mod lib; | ^^^ | = help: name the file either lib.rs or lib/mod.rs inside the directory "src/bin"
error: aborting due to previous error
For more information about this error, try `rustc --explain E0583`. error: could not compile `play`. warning: build failed, waiting for other jobs to finish... error: build failed