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

クレート

crate_type 属性は、クレートがバイナリかライブラリか(さらにはどの種類のライブラリか)をコンパイラに伝えるために使用でき、crate_name 属性はクレートの名前を設定するために使用できます。

ただし、Rust のパッケージマネージャーである Cargo を使用する場合、crate_type 属性と crate_name 属性のどちらもまったく効果がないことに注意することが重要です。Rust プロジェクトの大半では Cargo が使用されるため、これは crate_typecrate_name の実際の利用が比較的限定的であることを意味します。

// このクレートはライブラリです
#![crate_type = "lib"]
// このライブラリの名前は "rary" です
#![crate_name = "rary"]

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();
}

crate_type 属性を使用すると、--crate-type フラグを rustc に渡す必要がなくなります。

$ rustc lib.rs
$ ls lib*
library.rlib