编译的Rust可执行文件是否从依赖项中排除未使用的代码

如果我使用带有一些crate依赖项的Cargo构建一个Rust应用程序,那么我的应用程序未使用的那些依赖项中的任何代码是否会从最终的可执行文件删除
它看起来像.我并排测试了lib和bin箱子:
// hellobin/src/main.rs

extern crate hellolib;

fn main() {
    hellolib::func1();
}

对于lib:

// hellolib/src/main.rs

pub fn func1() {
    println!("Hello,world!");
}

pub fn func2() {
    println!("Hello,other world!");
}

构建我的二进制文件,然后用nm检查符号:

$nm target/debug/helloworld | grep hello
0000000100001360 t __ZN10helloworld4main17h749f61fb726f0a10E
00000001000014b0 T __ZN8hellolib5func117hec0b5301559d46f6E

只有used函数在最终二进制文件中有一个符号.

您可以使用货物rustc – -C link-dead-code编译,但您会看到两个符号都存在,包括未使用的符号:

$nm target/debug/helloworld | grep hello
0000000100001270 t __ZN10helloworld4main17h3104b73b00fdd798E
00000001000013d0 T __ZN8hellolib5func117hec0b5301559d46f6E
0000000100001420 T __ZN8hellolib5func217hc9d0886874057b84E

我相信(但我不确定)它是链接删除代码,所以它可能仍然被编译,然后在链接期间被删除.

相关文章

迭代器模式(Iterator)迭代器模式(Iterator)[Cursor]意图...
高性能IO模型浅析服务器端编程经常需要构造高性能的IO模型,...
策略模式(Strategy)策略模式(Strategy)[Policy]意图:定...
访问者模式(Visitor)访问者模式(Visitor)意图:表示一个...
命令模式(Command)命令模式(Command)[Action/Transactio...
生成器模式(Builder)生成器模式(Builder)意图:将一个对...