Crates.io | dyn_import |
lib.rs | dyn_import |
version | 0.0.1 |
source | src |
created_at | 2023-11-04 11:56:45.23847 |
updated_at | 2023-11-04 11:56:45.23847 |
description | Rust macro to import modules dynamically. |
homepage | https://github.com/protibimbok/rust-dyn-import |
repository | https://github.com/protibimbok/rust-dyn-import |
max_upload_size | |
id | 1025113 |
size | 6,643 |
dyn_import
is a Rust macro crate that simplifies the process of dynamically importing modules from a specified directory. This can be especially useful for projects that need to load and use modules at runtime, making your Rust application more flexible and extensible.
To use dyn_import
in your Rust project, add it as a dependency in your Cargo.toml
:
[dependencies]
dyn_import = "0.1"
Or run the following command:
cargo add dyn_import
Suppose you have the following directory structure for your Rust project:
my_project/
│
├── Cargo.toml
│
└── src/
├── main.rs
|-- some_mod.rs
└── some_mod/
├── module_one.rs
└── module_two.rs
Here's how you can use the import_mods macro from dyn_import in your Rust project:
extern crate dyn_import;
use dyn_import::import_mods;
// Declare the parent module
mod some_mod;
// All child modules are added here
import_mods!("src/some_mod");
fn main() {
// Dynamically import modules from the specified directory.
// The imported modules will be available as if they were part of your project.
// You can use them just like regular Rust modules.
// Example:
module_one::function_from_module_one();
module_two::function_from_module_two();
}