this post was submitted on 09 Jun 2025
89 points (94.9% liked)

Programmer Humor

36501 readers
145 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
top 9 comments
sorted by: hot top controversial new old
[–] [email protected] 5 points 6 days ago* (last edited 6 days ago)

To explain

There are 2 Error struct / enum declarations, probably in separate files

To the ?, they are different types and cannot be converted from one to the other (because they are two disparate structs that happen to have the same name, but can have different bodies)

To fix this

You can either use .some_func_result().map_err(|err| /* conversion here/*)?; +

Or you can impl From<Error1> for Error2

And you should also name it ThingError, so you can visually differentiate the two


+ There are like 10 different mapping functions, depending on if you're using an option or a result

I never remember which one specifically i need, (unwrap_or, map_or, map_or_else, ok, ok_or)

I usually just hunt through the auto complete list until i find the function signature that gives me what i need

[–] [email protected] 24 points 1 week ago (1 children)

Copied from the other repost:

Have you erased the continuation of the message that is saying something about "similar names, but are actually two distinct types"?

It is a common error if you have two dependecies that export the same third dependency and your code makes an assumption that the versions of the third dep match.

All other languages either straight-up don't support multiple versions of the same dep, or throw random errors at runtime. So this message is a consequence of rust supporting things that other langs only dream of.

[–] [email protected] 2 points 6 days ago

Hey thank you guys for your attempt to help, although I have already figured it out. I feel this is not the place for support requests, and my intention was rather just to share this funny error statement.

[–] [email protected] 22 points 1 week ago (1 children)

You really should avoid naming your type plain "Error"

[–] [email protected] 2 points 4 days ago (1 children)

Not a rust dev, rather c++, but just to understand, how should a type be named if it is used to carry information about an error?

Os this an issue with the language, similar to C where you don't have namespaces, and thus need to call everything acmecorp_error_v5 or something like that?

[–] [email protected] 1 points 4 days ago* (last edited 4 days ago) (1 children)

Prefix the name with what it's for. For example, I've previously got a SoundFontError from opening soundfont file.

"Error" is already used by std::error::Error. It might not be imported by the code that imports your error type, but I think it's better to give it distinct name.

The other thing is that you might want to use more than one library. Typical imports at the top of the file might look like this:

use bingbong::{BingBong, BingBongError, SomethingElse};
use bogos::binted::crotchidizer::{Crotchidizer, CrotchidizerError};

If both libraries named their error enums just "Error", the collision could be worked around, but it's an unnecessary extra step:

// Not sure how renaming affects compiler hints.
use bingbong::{BingBong, Error as BingBongError, SomethingElse};
use bogos::binted::crotchidizer::{Crotchidizer, Error as CrotchidizerError};

or if you want to avoid renaming:

use bingbong::{BingBong, SomethingElse};
use bogos::binted::crotchidizer::{self, Crotchidizer};

/* ... */

    match result {
        Ok(value) => return value,
        Err(bingbong::Error::MissionFailed) => panic!(),
        Err(bingbong::Error::UrMom) => todo!(),
        _ => unreachable!(),
    }
    
    if let Err(crotchidizer::Error::SomethingsWrong) = result2 {
        // ...
    }
    

If the screenshot had followed conventions, the message would say something like this:

could not convert error type `BingBongError` to `MyAppError`
[–] [email protected] 1 points 2 days ago

I see, thanks for the time you took explaining this. I think it is more a cultural difference then in how patterns evolved.

[–] [email protected] 11 points 1 week ago

If you run a build command on the CLI, it should tell you the full type names...

[–] [email protected] 3 points 1 week ago

skill issue tbh