Generic: expected type parameter `E`, found enum `MyError`

I'm currently trying to create a custom std::error::Error enum.
But when MyError is used in Generic, I get the following compilation error:

use std::{error::Error, fmt};

enum MyError {

impl fmt::Display for MyError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

impl Error for MyError {
    fn description(&self) -> &str {

fn test<E>()-> Result<(), E>
        E: Error
24 |     Err(MyError::Hello)
   |     --- ^^^^^^^^^^^^^^ expected type parameter `E`, found enum `MyError`
   |     |
   |     arguments to this enum variant are incorrect
   = note: expected type parameter `E`
                        found enum `MyError`

I have started to learn rust recently. I would be grateful if you could help me on this error. I've seen a lot of similar issues but for me it's difficult to relate to my problem.


Type parameters are chosen by the caller; what should happen if someone were to call it with test::<NotMyError>()? It wouldn't make sense. You should just return Result<(), MyError> instead.

1 Like

Thank you very much. It's clear now. :wink:

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.