-2

I have 3 structs in 3 different files, file tree:

├── Cargo.lock
├── Cargo.toml
├── README.md
├── src
│   ├── log_recorder
│   │   ├── log_listener.rs
│   │   ├── log_writer.rs
│   │   └── mod.rs
│   ├── main.rs
│   └── utils.rs
└── target

One struct holds the other two.

The log_recorder is implemented in the mod.rs:

pub mod log_listener;
pub mod log_writer;

struct log_recorder {
    writer: log_writer,
    listener: log_listener,
    num_of_recorder_threads: u8,
    run: bool,
}

impl log_recorder {
    fn recording_loop(&self) {
        // Do Something
    }
}

The log_listener is implemented in its own file and located in the log_recorder dir:

pub struct log_listener {
    redis_client: i8,
    run: bool,
}

impl log_listener {
    fn listening_loop(&self) {
        // Do Something
    }
}

The log_writer is implemented in its own file and located in the log_recorder dir:

pub struct log_writer {
    fd: i8,
}

impl log_writer {
    fn write(&self) {
        // Do Something
    }
}

I keep getting errors:

error[E0573]: expected type, found module `log_writer`
 --> src/log_recorder/mod.rs:6:13
  |
6 |     writer: log_writer,
  |             ^^^^^^^^^^ not a type
  |
help: consider importing this struct instead
  |


error[E0573]: expected type, found module `log_listener`
 --> src/log_recorder/mod.rs:7:15
  |
7 |     listener: log_listener,
  |               ^^^^^^^^^^^^ not a type
  |
help: consider importing this struct instead
  |

I saw this question 1, question2, but it didn't solve it for me.

cafce25
  • 15,907
  • 4
  • 25
  • 31
lior.i
  • 573
  • 1
  • 7
  • 20

1 Answers1

1

Inside mod.rs, log_writer and log_listener refer to the modules, not the types. You need to fully qualify them: log_writer::log_writer and log_listener::log_listener.

However, you should always follow Rust style conventions, which include using PascalCase naming for types. By that convention, your structs should be named LogRecorder, LogListener and LogWriter (while the modules stay with the same name as currently). Then, you can import them at the top of mod.rs:

use log_writer::LogWriter;
use log_listener::LogListener;

And use the unqualified names LogWriter and LogListener instead of the fully-qualified log_writer::LogWriter and log_listener::LogListener.

Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77