使用库

要将 crate 链接到这个新库,可以使用 rustc--extern 标志。库中的所有项目都会被导入到一个与库同名的模块下。这个模块的行为通常与其他模块相同。

// extern crate rary; // Rust 2015 版本或更早版本可能需要此声明

fn main() {
    rary::public_function();

    // 错误!`private_function` 是私有的
    //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()`