Rust by Example

11.1 Crates

The crate_type attribute can be used to tell the compiler whether a crate is a binary or a library (and even which type of library), and the crate_name attribute can be used to set the name of the crate.

// This crate is a library
#![crate_type = "lib"]
// The library is named "erty"
#![crate_name = "erty"]

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

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

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


When the crate_type attribute is used, we no longer need to pass the --crate-type flag to rustc.

$ rustc
$ ls lib*