extern crate
链接一个 crate 到这个新库,必须使用 extern crate
声明。这不仅会链接库,还会导入与库名相同的模块里面的所有项。适用于模块的可见性规则也适用于库。
// 链接到 `library`(库),导入 `rary` 模块里面的项
extern crate rary;
fn main() {
rary::public_function();
// 报错! `private_function` 是私有的
//rary::private_function();
rary::indirect_access();
}
# library.rlib 是已编译好的库的路径,假设在这里它在同一目录下:
# (原文:Where library.rlib is the path to to the compiled library,
# assumed that it's in the same directory here:)
$ rustc executable.rs --extern rary=library.rlib && ./executable
called rary's `public_function()`
called rary's `indirect_access()`, that
> called rary's `private_function()`
当前内容版权归 rust-lang-cn 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 rust-lang-cn .