Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
435 views
in Technique[技术] by (71.8m points)

Trying to get Rust to load files

I'm having trouble trying to get Rust to load files in subdirectories. Rust treats the files as the modules and the code as part of the module, but I'm used to Ruby's way of treating files and directories separate from the code that they contain.

src/main.rs

mod lib {
    pub mod manifest;
}

src/lib/manifest.rs

mod structs {
    pub mod entity;
}

src/lib/structs/entity.rs

pub struct entity {
    type: String
}

The error I get is:

error: cannot declare a new module at this location
 --> src/lib/manifest.rs:2:13
  |
2 |     pub mod entity;
  |             ^^^^^^
  |
note: maybe move this module `structs` to its own directory via `structs/mod.rs`
 --> src/lib/manifest.rs:2:13
  |
2 |     pub mod entity;
  |             ^^^^^^
note: ... or maybe `use` the module `entity` instead of possibly redeclaring it
 --> src/lib/manifest.rs:2:13
  |
2 |     pub mod entity;
  |             ^^^^^^
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The best way to fix this is to rename the manifest file to mod.rs and change the first line in main.rs to:

mod lib;

and change mod.rs to:

pub mod structs {
    mod entity;
}

I think the reason for your error is because there's a manifest.rs file but no folder. Why this causes the subfolder files to not load I can't say, I'm still new to Rust myself so there's a good chance I'm wrong.

Someone with more Rust knowledge might know, but since they're downvoting instead of being helpful, I doubt you'd get a better answer.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...