Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

ライブラリを使用する

この新しいライブラリにクレートをリンクするには、rustc--extern フラグを使用できます。その すべてのアイテムは、ライブラリと同じ名前のモジュールの下にインポートされます。 このモジュールは通常、他のモジュールと同じように動作します。

// extern crate rary; // Rust 2015 エディション以前では必要になる場合があります

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

    // エラー! `private_function` はプライベートです
    //rary::private_function();

    rary::indirect_access();
}
# library.rlib はコンパイル済みライブラリへのパスで、ここでは同じ
# ディレクトリにあると仮定します:
$ rustc executable.rs --extern rary=library.rlib && ./executable
called rary's `public_function()`
called rary's `indirect_access()`, that
> called rary's `private_function()`