crate

crate_type 属性可以告知编译器 crate 是一个二进制的可执行文件还是一个库(甚至是哪种类型的库),crate_time 属性可以设定 crate 的名称。

  1. // 这个 crate 是一个库文件
  2. #![crate_type = "lib"]
  3. // 库的名称为 “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. }

当用到 crate_type 属性时,就不再需要给 rustc 命令加上 --crate-type 标记。

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