Rust compilation unit - Crates

CK CK 3788 
Created at
Updated at  
252 0 0

A crate is a compilation unit in Rust. Whenever rustc some_file.rs is called, some_file.rs is treated as the crate file. If some_file.rs has mod declarations in it, then the contents of the module files would be inserted in places where mod declarations in the crate file are found, before running the compiler over it. In other words, modules do not get compiled individually, only crates get compiled.

A crate can be compiled into a binary or into a library. By default, rustc will produce a binary from a crate. This behavior can be overridden by passing the --crate-type flag to lib.

 

Creating a Library

Let's create a library, and then see how to link it to another crate.

 

In rary.rs:

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

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

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

    private_function();
}

 

$ rustc --crate-type=lib rary.rs
$ ls lib*
library.rlib

Libraries get prefixed with "lib", and by default they get named after their crate file, but this default name can be overridden by passing the --crate-name option to rustc or by using the crate_name attribute.

 

Using a Library

To link a crate to this new library you may use rustc's --extern flag. All of its items will then be imported under a module named the same as the library. This module generally behaves the same way as any other module.

// extern crate rary; // May be required for Rust 2015 edition or earlier

fn main() {
    rary::public_function();

    // Error! `private_function` is private
    //rary::private_function();

    rary::indirect_access();
}

 

# Where library.rlib is the path to the compiled library, assumed that it's
# in the same directory here:
$ rustc executable.rs --extern rary=library.rlib && ./executable 
called rary's `public_function()`
called rary's `indirect_access()`, that
> called rary's `private_function()`
Tags Rust Compilation Rust Library crate Facebook X
Comments 0
Similar posts
  1. Rust Attributes for conditional compilation, crate, lints, mark functions, macros
    313
  2. Rust Std misc
    372
  3. Run a specific module in a external file with Rust
    568
  1. Basic Rust Thread Example to Perform Concurrent Computation
    614
  2. Multi-threaded HLS Server in Rust
    767
  3. Question(?) Operator in Rust
    569
  4. Rust code example to access MySQL
    1,098
  5. Rust Documentation
    312
  6. Rust Compatibility
    718
  7. Rust Unsafe Operations
    362
  8. Rust Testing
    1,234
  9. Rust Std library types
    368
  10. Rust Error Handling
    342
  11. Rust Macro Rules
    1,508
  12. Rust mechanism for defining shared behavior across different types - Traits
    1,762
  13. Rust Scoping Rules - only accessible within the block of code
    1,066
  14. Rust Generics - Generalizing types and functionalities
    1,349
  15. Rust Package Management Tool - cargo
    307
  16. Rust Modules - Visivility, Struct Visibility, Use Declaration, super and self, File Hierarchy
    1,889
  17. Rust functions - Methods, Closures, Capturing
    1,198
  18. Rust Flow of Control - if/else, loop. while, for and range, match, if let, let-else, while let
    335
  19. Rust Expressions
    924
  20. Rust Conversion - From, Into, TryFrom, TryInto
    1,309
  21. gRPC library for Rust
    343
  22. Rust Data Type Operations - Casting, Literals, Inference, Aliasing
    326
  23. Variable Bindings in Rust
    966
  24. Rust Custom Data Types - struct, enum, use, constants
    298
  25. Arrays and Slices in Rust
    338
  26. Tuples in Rust - a collection of values of different types
    297
  27. Literals and operators in Rust
    296
  28. Rust Data Type - What is mutable?
    256
  29. Rust Code Testing Environment without installing anything on your computer
    324
  30. Rust Thread Example
    346
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    42
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    36
  3. How do I create animated background for Google Meet?
    21,039
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    252
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    94
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    157
  7. Array of arguments passed to script in PHP
    90
  8. Invalid Auction Storms in Ad Tech
    98
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    202
  10. The Two Clocks of Ad Server Architecture
    70
  11. Frequency Cap Architecture Overview
    515
  12. Collecting Proof-of-Play from 100,000 Screens
    146
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    288
  14. What is RedisVL?
    109
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    232
  16. TPU & GPU Cluster Management Tools
    245
  17. The Concept of Scaffolding in AI Agents
    234
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    583
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    259
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    339
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    202
  22. Machine Learning: Training vs. Ranking
    222
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    222
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    195
  25. Designing a Data Curation System Around a Vector Database
    221
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    256
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    656
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    196
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    219
  30. Understanding Data Lineage
    263