51 lines
1.6 KiB
Plaintext
51 lines
1.6 KiB
Plaintext
---
|
|
title: Modules & Imports
|
|
description: Module declarations, imports, re-exports, include directives, and module resolution rules.
|
|
icon: FolderTree
|
|
---
|
|
|
|
Mist's module system was created to fix Rust's dreaded parent-declared module system, files declare themselves and their publicity via `pub module my_mod;` as the first statement.
|
|
|
|
|
|
```mist
|
|
// Declare a module
|
|
pub module foo;
|
|
|
|
// Import
|
|
use std::collections::HashMap;
|
|
|
|
// Re-exporting import (alias)
|
|
pub use my_module::MyType;
|
|
```
|
|
|
|
### Include Directives
|
|
|
|
Mist supports C-style include directives for importing external files and crates:
|
|
|
|
```mist
|
|
// Global include (C-style)
|
|
#include <stdio.h>
|
|
|
|
// Use include (import from crate)
|
|
pub use my_crate::my_module;
|
|
|
|
// Local include (import from relative path)
|
|
#include "my_local_module.mist"
|
|
```
|
|
|
|
Include directives allow you to bring in external code without using the module system, which is useful for C interop and local file organization.
|
|
|
|
### Module Resolution
|
|
|
|
Mist maps the module tree to Rust's module system:
|
|
|
|
| Mist Path | Rust Output |
|
|
|------------------------------|------------------------------|
|
|
| `src/main.mist` | `.mist/src/main.rs` |
|
|
| `src/utils/package.mist` | `.mist/src/utils/mod.rs` |
|
|
| `pub module foo;` | `.mist/src/foo.rs` |
|
|
|
|
A `package.mist` file acts as a directory's module root, analogous to `mod.rs`, but it's not required, as long as the directory contains `.mist` files, it will auto generate `mod.rs`.
|
|
|
|
#### *💡 Tip*: File names don't matter, `module my_mod` determines the name.
|