Mismatched types when types are the same but paths differ for some reason

I'm trying to modularize a program while learning the rust module system. Any idea why this will not compile, why the type/path mismatch?

/main.rs

mod wsl;
use wsl::inv::invoker::Invoker;
use wsl::inv::invocablecategorylist::InvocableCategoryList;

fn main() {
    let config = wink::WinkConfig::get_from_cmd_line_args();
    let category_list = InvocableCategoryList::get();
    if !config.help_msg.is_empty() {
        wink::help(
            &config.help_msg.to_string(),
            config,
            category_list.categories,
        );
//...

Compilation error:

    error[E0308]: mismatched types
    --> src/main.rs:46:13
     |
  46 |             category_list.categories,
     |             ^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `wink::wsl::inv::invocablecategory::InvocableCategory`, found struct `InvocableCategory`
     |
     = note: expected struct `Vec<wink::wsl::inv::invocablecategory::InvocableCategory>`
                found struct `Vec<InvocableCategory>`

/lib.rs

mod wsl;
use crate::wsl::inv::invocablecategory::InvocableCategory;                
//...
pub fn help(msg: &str, config: WinkConfig, mut categories: Vec<InvocableCategory>) {

/inv.rs

pub mod invocable;
pub mod invocablecategory;
pub mod invocablecategorylist;
pub mod invoker;

I would prefer to keep these types in wsl::inv but need to keep them in separate files, hence the nested path.

/wsl/inv/invocable.rs defines Invocable struct:

// no mod/use

/wsl/inv/invocablecategory.rs defines InvocableCategory struct

use crate::wsl::inv::invocable::Invocable;

/wsl/inv/invocablecategorylist.rs defines InvocableCategoryList struct

use crate::wsl::get_config_file_path;
use crate::wsl::inv::invocablecategory::InvocableCategory;
//...
pub struct InvocableCategoryList {
    /// The categories field contains the list of InvocableCategory.
    pub categories: Vec<InvocableCategory>,
}

impl InvocableCategoryList {
    /// Return an InvocableCategoryList populated from a hard-coded list of categories
    /// plus the contents of $HOME/.wink.json (WSL) or $USERPROFILE/wink.json (Windows).
    pub fn get() -> InvocableCategoryList {
        let mut category_list = InvocableCategoryList {
            categories: Vec::new(),
        };

/wsl/inv/invoker.rs: defines Invoker struct

use crate::wsl::inv::invocable::Invocable;
use crate::wsl::wsl_path_or_self;

I can provide the entire source code as a zip if that would help, but it's a bunch of files I don't want to check it in with git until I get it working.

You should not have mod wsl; in both main.rs and lib.rs. Each time you write mod foo, the code for that module gets pasted into the program at that point. If you include the same module twice, you end up with two distinct modules that happen to have the same source code.

Instead, each module should be declared exactly once, in its parent module or crate, usually in your library (lib.rs and its submodules).

In main.rs and any of its submodules, remove the mod wsl; line, and import items from your library (wink) instead:

use wink::wsl::invoker::Invoker;
use wink::wsl::inv::invocablecategorylist::InvocableCategoryList;

Awesome, that did it! Thanks, especially for the quick response! I think these were the key changes.

/main.rs

use wink::wsl::inv::invoker::Invoker;
use wink::wsl::inv::invocablecategorylist::InvocableCategoryList;

/lib.rs

pub mod wsl;
use crate::wsl::inv::invocablecategory::InvocableCategory;

Resolving Rust error[E0308] Mismatched Types When Types Actually Match – rustapopoulos.com

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.