» Quick Introduction to Rust » 2. Advanced » 2.1 Crates

Crates

A crate is a compilation unit in Rust. A crate can be compiled into a binary or into a library.

Create a Library

In abc.rs:

pub fn a() {
    println!("abc's `a()`");
}

fn b() {
    println!("abc's `b()`");
}

pub fn c() {
    print!("abc's `c()`, which called \n");
    private_function();
}

If you have rust installed locally, try the build command:

$ rustc --crate-type=lib abc.rs
$ ls lib*
libabc.rlib

Libraries get prefixed with "lib", and by default they get named after their crate file.

Use a Library

fn main() {
    abc::a();

    // [error] `b` is private
    abc::b();

    abc::c();
}