Dead code elimination for libraries


For my first project, I think I “optimized” a bit too early by creating a workspace with separate crates… besides the added complexity, I realize now it might lead to polluting the namespace unnecessarily.

I think I can instead make it one crate with separate modules.

Does rust aggressively apply dead-code elimination to a binary such that when including modules, only what’s actually used gets included?

e.g. if I import globs like foo::*, it won’t actually link in everything in foo, and if I include foo::{Bar,Baz} and both don’t use Baz and ignore dead-code warnings, it won’t link in Baz?


That’s usually performed by linker not compiler. And afaik all linkers that Rust uses for major platforms do it.

1 Like

If it matters - specifically thinking of webassembly