Way to avoid listing all generic parameters?

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=b8882d9b94a75eceac9183442c824d2f

pub fn do_thing<T, S: AsRef<str>>(s: S) {
}

fn main() {
    do_thing::<i32>("hello"); //< I'd like this to work but it doesn't, Rust tells me I have to specify both
                              //< type parameters because I've specified one of them.
}

Reduced example here, imagine the t: T parameter is used internally in the function. Key point is that I have to explicitly specify T as e.g. ::<i32> but I would like to avoid having to specify &str for S since the compiler knows what that type is.

One possible workaround is to have a dummy parameter of type T that is just used to get the compiler to fill out that type parameter, but I'd like to avoid that if possible.

You can have it inferred by writing:

do_thing::<i32, _>("hello");

But default type parameters are not a thing on functions at this time. There's some discussion in this tracking issue.

2 Likes

Ah of course, thanks. That's probably ok for me for now.

You can also use existential universal types to remove the S type parameter.

pub fn do_thing<T>(s: impl AsRef<str>) {
}

In this case, it'd be impossible to choose T explicitly at all, so it's probably not what OP would like.

2 Likes