Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rust Import structure from another file [duplicate]

Tags:

import

rust

Here's my file structure:

src
├── [2.4K]  game_of_life.rs
├── [1.7K]  main.rs
└── [1.9K]  preset.rs

File contents: preset.rs:

pub struct Preset {
    ...
}

impl Preset {
    ...
}

Error I got

I tried importing Preset in main.rs:

mod preset;

And it worked fine.

After that I tried to import Preset in game_of_life.rs using the same line of code, but got an error:

Error: file not found for module `preset`
Help: name the file either game_of_life/preset.rs or game_of_life/preset/mod.rs inside the directory "src"

I don't understand why Rust is trying to find the preset in game_of_life.

What I tried to do to fix it

I found, that I have to wrap Preset in pub mod preset, but this didn't help me.


1 Answers

I found an answer: I just had to use use crate::preset::Preset. To understand how and why this works read the first comment under my question.