使用库
要将一个 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()`