I have about a hundred fields in the structure, so I want to store the struct definition and the data in separate files.
Specifically, I want to keep the definition in a file, and the default implementation in another file. I want to access the default implementation from main.rs. I am not sure how to achieve it.
Directory tree
.
├── Cargo.toml
├── src
│ ├── config.rs
│ ├── lib
│ │ └── config_types.rs
│ └── main.rs
config_types.rs
#[derive(Debug, Copy, Clone)]
pub struct Config {
a: u8,
b: u8,
}
impl Config {
pub fn new(a: u8, b: u8) -> Self {
Config { a, b }
}
}
config.rs
#[path = "./lib/config_types.rs"] mod config_types;
use config_types::Config;
impl Default for Config {
fn default() -> Self {
Config {
a: 0b0000_0001,
b: 0b0000_0000,
}
}
}
main.rs
#[path = "./lib/config_types.rs"] mod config_types;
#[path = "./config.rs"] mod config;
use config_types::Config;
fn main() -> ! {
let config = Config::default();
}
The error I am getting:
error[E0599]: no function or associated item named `default` found for struct `config_types::Config` in the current scope
--> src/main.rs:64:26
|
64 | let config = Config::default();
| ^^^^^^^ function or associated item not found in `config_types::Config`
|
::: src/./lib/config_types.rs:2:1
|
2 | pub struct Config {
| ----------------- function or associated item `default` not found for this
|
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following trait defines an item `default`, perhaps you need to implement it:
candidate #1: `Default`
If I move the default implementation into config_types.rs, the error goes away.
How can I achieve this?