使用库
要将 crate 链接到这个新库,可以使用 rustc
的 --extern
标志。然后,它的所有项都将导入到一个与库同名的模块下。此模块的行为通常与任何其他模块相同。
// extern crate rary; // May be required for Rust 2015 edition or earlier
fn main() {
rary::public_function();
// Error! `private_function` is private
//rary::private_function();
rary::indirect_access();
}
# Where library.rlib is the path 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()`