The Missing Bit

TIL - Rust crate lib and binaries are separate compile unit
May 6, 2020

til rust

Given a rust crate with three files:

lib.rs
main.rs
common.rs

When lib.rs and main.rs are compiled, they are each compiled in their own independant compile unit. This means that we have to put mod common inside each file.

But the better approach is to put mod common inside lib.rs and do use my_crate_name::common inside main.rs or other binaries within the crate.

If you wish to comment or discuss this post, just mention me on Mastodon or Twitter.

Related posts