Links are [ text ] ( target ) ; you have it switched.
Okay. So, name resolution is a little weird and they’re working on making it less so, but in the meantime:
- only write
extern crate
in lib.rs
or main.rs
; not in any submodules. Doing so causes Weird Things to happen to imported items, ESPECIALLY macros.
- in
main.rs
, don’t import modules from your library directly. Do extern crate audiobookfinder;
to pull in the library structure and then use pieces from that. This way, lib.rs
can handle all the crate imports and you don’t have to redo them in main.rs
except for the crates that main.rs
directly needs.
// lib.rs
#[macro_use] extern crate serde_derive;
extern crate serde;
extern crate tree_magic;
extern crate uuid;
extern crate id3;
// data/mod.rs (can be just data.rs if you're not going to have data/ child mods)
use uuid::Uuid;
#[derive(Serialize, Deserialize)] // works now
// main.rs
extern crate audiobookfinder as abf;
// no mod statements
use abf::data;
use abf::data::Collection;
// etc.
The executable and the library are, technically, separate compilation units within the crate. If you pull in your submodules directly into main.rs
, then main.rs
has to re-declare all the crate imports from lib.rs
.
This is because, despite how it’s popular to dunk on C/C++ #include
being a copy/paste mechanism … mod
is a copy-paste mechanism. mod file
is exactly equivalent to mod file { /* contents of that file */ }
So, the “can’t find Serialize” error was occurring because when src/data/mod.rs
got dumped into src/main.rs
, main.rs
didn’t have Serialize in scope.
In main.rs
, extern crate audiobookfinder;
pulls the lib/
tree as a foreign, standalone artifact, and doesn’t include the source code directly, it just makes symbols available.
TLDR
- In this particular case, you can probably get away with killing
lib.rs
and bringing all the crate imports from that into main.
- If you have a main/lib split, main should always import lib as a crate, and not redeclare submodules from lib.
-
extern crate
should only be done in crate roots, lib.rs
or main.rs
.