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.

However, it is important to note that both the crate_type and crate_name
attributes have no effect whatsoever when using Cargo, the Rust package
manager. Since Cargo is used for the majority of Rust projects, this means
real-world uses of crate_type and crate_name are relatively limited.

  1. // This crate is a library
  2. #![crate_type = "lib"]
  3. // The library is named "rary"
  4. #![crate_name = "rary"]
  5. pub fn public_function() {
  6. println!("called rary's `public_function()`");
  7. }
  8. fn private_function() {
  9. println!("called rary's `private_function()`");
  10. }
  11. pub fn indirect_access() {
  12. print!("called rary's `indirect_access()`, that\n> ");
  13. private_function();
  14. }

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

  1. $ rustc lib.rs
  2. $ ls lib*
  3. library.rlib