Why Ok has two generic types?

Ok is one of Enum Result, and has one generic type bound. But why hyper seem to use two types? Ok::<_, Infallible>

enum variants have the same amount of generic parameters as the enum they stem from.

Basically, Ok::<T, E>(…) is sugar for Result::<T, E>::Ok(…). In both cases the Ok(…) will be creating a value of type Result<_, _>, and the second type parameter lets us specify, at construction time, the desired Error type, in a handily succint manner :slightly_smiling_face:

1 Like

You have to understand that an enum variant is not a type in itself, because that doesn't make much sense as a first approximation. (There is an RFC for treating them as such, but that's more like syntactic sugar for refinement types, and is not relevant to this discussion.)

An enum variant is best thought of as a constructor or a function that produces a value of the given enum type. The key point is that all variants of a given enum have the same type: the type of the enum itself. So they must carry around the same generic arguments. Imagine Ok and Err to be functions of the form:

fn Ok<T, E>(inner: T) -> Result<T, E> { … }
fn Err<T, E>(inner: E) -> Result<T, E> { … }

As you can see, since both functions return Result<T, E>, it would not be possible for them to ignore generic arguments, because then how would the complete type of the resulting Result value be known?