Rust's module system, pub, and re-exports.
src/
├── main.rs
└── exchanges/
├── mod.rs
└── binance/
├── mod.rs
└── client.rs
exchanges/binance/[mod.rs](<http://mod.rs>)
mod client;
pub use client::BinanceClient; // Re-export
exchanges/[mod.rs](<http://mod.rs>)
pub mod binance;
pub use binance::BinanceClient; // Re-export again
mod exchanges;
use exchanges::BinanceClient; // Now works!
use exchanges::binance::BinanceClient; // Full path, still works
They are separate!
pub struct User { // Struct is public
id: u64, // Field is PRIVATE (default)
pub name: String, // Field is PUBLIC
}
pub on... |
Who can see it |
|---|---|
struct only |
Type visible everywhere, fields only in same module |
struct + field |
Type and that field visible everywhere |
| Neither | Only visible within same module |
// src/models/user.rs
pub struct User {
id: u64, // No pub
}
impl User {
pub fn get_id(&self) -> u64 {
self.id // ✓ Works—same module
}
}