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/
  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 (/, /, /, /wsl/, /wsl/inv/<4 types>.rs), but it is important to reference everything properly, and apparently only once.

I had this in /

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

I had this in /

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

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

mod wsl;

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

The solution was to remove mod wsl; from / and update the corresponding line in / 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 / 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).


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: Logo

You are commenting using your 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: