Rust
Module System
Modules, paths, and visibility.
By EZ4Code Team
modulemodule
Code
// mod.rs or main.rs
mod network {
pub mod server {
pub fn start() {
println!("Server starting...");
connect("localhost");
}
fn connect(addr: &str) {
println!("Connecting to {}", addr);
}
}
pub mod client {
use super::server; // Access parent module
pub fn send() {
println!("Sending data...");
}
}
}
// Import with use
use network::server;
use network::client;
// Re-export
pub use network::server::start as run_server;
// External crate
// use std::collections::HashMap;
fn main() {
server::start();
client::send();
run_server();
// Nested path
use std::io::{self, Read, Write};
// Glob import
// use std::collections::*;
}Explanation
mod defines modules; pub controls visibility; use imports paths; super/crate/self reference relative paths.