Resolving Rust error[E0308] Mismatched Types When Types Actually Match

This blog post explains one possible solution for rust compilation error E0308, mismatched types, when the referenced types actually match.

I was getting this 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>`

My project structure is not very complex (/main.rs, /lib.rs, /wsl.rs, /wsl/inv.rs, /wsl/inv/<4 types>.rs), but it is important to reference everything properly, and apparently only once.

I had this in /main.rs.

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

I had this in /lib.rs.

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

Do you see the issue? I obviously did not. Both /main.rs and /lib.rs contained the following line.

mod wsl;

This causes both the binary crate (the executable defined by /main.rs) and the library crate (defined by /lib.rs) to do something with the contents of /wsl.rs in both places, which leads to the mismatch.

The solution was to remove mod wsl; from /main.rs and update the corresponding line in /lib.rs with the pub modifier to make it available to other crates that use the library, such as the binary crate.

pub mod wsl;

I may have had to update references in /main.rs to include an absolute path for to access things in wsl, where wink is the name of my project.

if !wink::wsl::is_windows_or_wsl() {

I intend to write a more complete post about project/file/path structure, as I faced some other challenges, especially trying to use separate files for types that I wanted to appear in a single path (rust equivalent of C# namespace).

Credit

One thought on “Resolving Rust error[E0308] Mismatched Types When Types Actually Match

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: