About From<Vec<T>> ,How to handle this situation?

I has a struct Query like HashMap, and I want to create From<Vec<(key,value)>> for it,this is my code: oss-rs/types.rs at master · tu6ge/oss-rs (github.com)

and I have a function like :

fn foo<Q: Into<Query>>(query: Q) {

now, I can call this function with vec![("max-keys", "5"), ("prefix", "babel")], it is works.

but, I try to call this with vec![] , it is not work.

sometime, I want call the function without query . how to I update this From sentence ?

What is the error message?

I'm guessing that type inference doesn't know what K and V to use for your empty vec![]. It may seem like this doesn't matter, but the compiler still needs a concrete Vec<(K, V)> type in order to resolve the rest of your code.

You could use the explicit "turbofish" syntax on your call, like foo::<Vec<&str, &str>>(vec![]). Or if you can construct an empty Query directly, you can just use that like foo(Query::new()).


cannot infer type for type parameter K or V

Yeah, I am seeking From<Vec<T>> coexist with From<Vec<(K, V)>> .

Ohhh, my english level is very low.

Playground illustration of what I believe is your issue.

1 Like

Thank you very mush

perhaps not better way, I plan to add this conversion:

impl From<Vec<()>> for Query {
    fn from(_: Vec<()>) -> Self {

now, I can call foo with vec![();0]

Or maybe just add an impl for [(); 0], and you can then call foo([]), which is a little less noisy. It's a good idea to not require unconditional allocation anyway.

1 Like

Pretty, why didn't I think of it

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.