]> git.proxmox.com Git - rustc.git/blob - src/doc/rust-by-example/src/attribute/crate.md
New upstream version 1.38.0+dfsg1
[rustc.git] / src / doc / rust-by-example / src / attribute / crate.md
1 # Crates
2
3 The `crate_type` attribute can be used to tell the compiler whether a crate is
4 a binary or a library (and even which type of library), and the `crate_name`
5 attribute can be used to set the name of the crate.
6
7 However, it is important to note that both the `crate_type` and `crate_name`
8 attributes have **no** effect whatsoever when using Cargo, the Rust package
9 manager. Since Cargo is used for the majority of Rust projects, this means
10 real-world uses of `crate_type` and `crate_name` are relatively limited.
11
12 ```rust,editable
13 // This crate is a library
14 #![crate_type = "lib"]
15 // The library is named "rary"
16 #![crate_name = "rary"]
17
18 pub fn public_function() {
19 println!("called rary's `public_function()`");
20 }
21
22 fn private_function() {
23 println!("called rary's `private_function()`");
24 }
25
26 pub fn indirect_access() {
27 print!("called rary's `indirect_access()`, that\n> ");
28
29 private_function();
30 }
31 ```
32
33 When the `crate_type` attribute is used, we no longer need to pass the
34 `--crate-type` flag to `rustc`.
35
36 ```shell
37 $ rustc lib.rs
38 $ ls lib*
39 library.rlib
40 ```