Open In App

Rust – Crate_type Attribute

Last Updated : 02 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In Rust, we have a concept of crates. Crates are basically libraries or packages as defined in other programming languages. Cargo, the rust package management tool helps ship the crates of our program to other programs’ cargo.  Crates produce an executable library where every crate has a root module that has the code for the crate. 

The crate_type attribute in Rust is used for inferring whether the crates in the program are binary or can be used to tell whether the compiler in a crate is a binary or a library and the crate_name_type is used for naming the crates.

Here, to implement the crate_name_type we declare the name in the .toml file in the rust compiler dependencies file:

[package]
name = "gfg"
version = "0.1.0"
authors = ["runner"]
edition = "2021"
[dependencies]

 

Example 1:

Rust




#![crate_type = "lib"]
#![crate_name = "gfg"]
#[allow(dead_code)]
fn main(){
pub fn pub_func() {
    println!("calling GFG `pub_func()`");
}
fn pvt_func() {
    println!("calls GFG `pvt_func()`");
}
  
pub fn new_access() {
    print!("called GFG `new_access()`, that\n> ");
  pvt_func();
 }
pub_func();
pvt_func();
new_access();
}


Output:

 

Explanation:

In this example, we have declared three functions pub_func ( public function), pvt_func ( private function), and an indirect access function known as the new_access(). The crate type is declared as a lib and the crate_name is defined as gfg in the cargo.toml package dependencies file. After we declare and define the functions we call the three functions respectively and we see the output. 


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads