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

ライブラリの作成

ライブラリを作成し、それを別のクレートにリンクする方法を見てみましょう。

rary.rs では:

pub fn public_function() {
    println!("called rary's `public_function()`");
}

fn private_function() {
    println!("called rary's `private_function()`");
}

pub fn indirect_access() {
    print!("called rary's `indirect_access()`, that\n> ");

    private_function();
}
$ rustc --crate-type=lib rary.rs
$ ls lib*
library.rlib

ライブラリには "lib" という接頭辞が付き、デフォルトではクレートファイルにちなんだ名前が付けられますが、このデフォルト名は、rustc--crate-name オプションを渡すか、crate_name 属性を使用することで上書きできます。