Struct in module, used in another module : E0308

I am trying to learn Rust but it is fighting me every step of the way. I have the following code (in a module called events.rs):

let search : softtube::Search = match text.len() {
                0 => softtube::Search::Limit(-1),
                _ => softtube::Search::Text(text)
            };
app.videos.search(Some(search));  <-- Error occurs here!!

Search is defined in a module called softtube.rs:

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Search {
    Limit(i32),
    Text(String)
}

And now I am getting the error :

events::softtube::Search
let search : softtube::Search = match text.len() { ... }
mismatched types

expected enum `softtube::Search`, found enum `events::softtube::Search`

note: expected type `softtube::Search`
found type `events::softtube::Search`rustc(E0308)

So it says on the second row of the error message that search is of the type softtube::Search, but later, on the last row it says it is the type events::softtube::Search!?! Why Rust? Why are you punishing me?

The first couple of weeks I wrote all Rust code in main.rs just to avoid crap like this, but now I have to bite the bullet and finally learn how to do this. So can anyone help me with this? Please?

Do you have Search defined multiple times in different modules?

No, but I do have a couple of lower case functions and variables named 'search', can that cause this problem?

Like @RustyYato, I can just guess. It's better if you can provide minimal code that can compile that produce the same error. When trying to reproduce with a minimal code, I tend to find the problem myself. This may help some how?

1 Like

@limira, I will try that, but in my case they are actually different files. Doesn't that matter?

I link them in with this syntax (for some reason I don't need the #[path="path"] in main.rs):

#[path="softtube.rs"]
mod softtube;

No, you just do this:

// in main.rs
mod mod1; // and a file `./mod1.rs` or `./mod1/mod.rs`
mod mod2;
mod mod3;

If I don't use the path-syntax (in files other than main.rs), I get this error:

src/misc.rs
file not found for module `misc`
help: name the file either events/misc.rs or events/misc/mod.rs inside the directory "src"rustc(E0583)

But good news, my problem seemed to be the missing 'crate::' in front of softtube::Search() that you used in the playground example! Thanks for that!

I can't figure out how to make the playground link, but I just needed to add the crate::...

I think you place your files in wrong folders/directories. If they are all in their correct directories, you don't have to #[path="softtube.rs"] (In fact, this the first time I see it).

If you want to avoid the crate::, bring it into scope by using use ...;

use some::path::to::EnumItem;
let item = EnumItem::Variant;

Well, I created a project with 'cargo new' and then place all my files inside the src folder, isn't that correct?

So basically

src/

  • main.rs
  • events.rs
  • softtube.rs
  • ....

cargo.toml
...

If every mod are in main.rs:

// main.rs
mod mod1;

You have two options for the file (that contains code for your mod1):

src/mod1.rs
// or 
src/mod1/mod.rs

But if you want a mod2 in module mod1:

// in mod1.rs (or mod1/mod.rs)
mod mod2;

You have to place the file in:

src/mod1/mod2.rs //(The file is in folder: src/mod1)
// or
src/mod1/mod2/mod.rs
2 Likes

I am confident that I have completely misunderstood a whole lot of things, but anyway here are my code simplified a lot : https://github.com/Hultan/softtube-error

  1. Go to events.rs and comment out '#[path="softtube.rs"]' and you will get the module error I talked about.
src/misc.rs
file not found for module `misc`
help: name the file either events/misc.rs or events/misc/mod.rs inside the directory "src"rustc(E0583)
  1. Go to events.rs and remove the three 'crate::' and you will get the error message I originally asked about. But it works with the 'crate::' so I am happy with that for now, but just in case you wanted to see exactly how I have messed this up :slight_smile:

About my use of the 'mod' keyword in main.rs and events.rs, I have a suspicion that I am using that very wrong. The only thing I am trying to achieve here at this moment is to not have all the code in one file (main.rs). I am not trying to build a "library" (cargo new --lib) or anything...

A few YouTube-videos later and I realized that you can write :

use crate::module;

and

use super::module;

That makes things easier... :slight_smile:

I prefer to learn things by doing, until I go completely mad, and then I do what I should have done from the beginning. Read the manual, and learn from others...

Thanks everybody who helped me...

Yeah, I understand that. But I think read The Book first is a good idea. It helps me very much.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.