Multiple error types


#1

I have a function like so:

fn foo(bar: Result<String, ErrorType1>) { 
    match bar {
        Ok(x) => ...
        Err(e @ ErrorType1) { ...
        Err(e) { ...

Is there any way I can rework this to support bar being a Result type that can either be ErrorType1 or ErrorType2 and do Err(e @ ErrorType1) and Err(e @ ErrorType2) in my function?


#2

If you want a return value that could possess multiple values, you would need to have an enum, which is a type that can represent multiple values. In your case, it would be something like

enum ErrorEnum {
  Error1(ErrorType1),
  Error2(ErrorType2)
}

If you implement From interfaces as seen in this article https://doc.rust-lang.org/stable/book/first-edition/error-handling.html#composing-custom-error-types (supposedly outdated, but I still like it), then it might make working with these errors more ergonomic.