Specifying only a subset of generic types

So I am using the nom combinator map and, for reasons that I fully understand, the compiler is unable to determine the error type E, though I know what it should be. Since map has six generic types and the compiler needs help with just one of them, is there a way to specify just this type without all the others since this would be very tedious?

I know one way is to save the returned IResult to a variable so that I can specify its type but is this really necessary? I don't really have any need to do this except to be able to tell the compiler the error type. To be more specific, my enclosing function returns a Result with an error type to which I can convert the desired map error type by having implemented the From trait. So I'd like to be able to use the ? operator on the map result, but this is why the compile doesn't know the map error type.

you can use placeholders for generic types that can be inferred like so: map<_, _, _, E, _, _>(.

Regular Result also lets you do something like res.map_err(|e: E| e)? which also lets you specify the error type without a separate variable definition.

Whichever option looks best to you is subjective.

1 Like

Yeah I'm not sure why I didn't think of that! I suppose it's because I've used placeholders plenty of times for variables that I didn't need to use, but I'd not really needed to use them for inferred generic types before. Thanks for the answer!

1 Like

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.