0
votes

I'm using the serde_json crate and have to make a type annotation for the return value of serde_json::from_slice(). If I use a let and a match statement afterwards, this works

let n: Result<serde_json::Value, serde_json::Error> = serde_json::from_slice(buf);
match n {
    Ok(_i) => (),
    Err(_e) => (),
};

But as I don't need n, I don't want to make the let statement in the first place, and only use a single match pattern. How can I make the type annotation for _i?

1

1 Answers

3
votes

You can explicitly specify the type parameters of a function call by using this syntax func::<T>().

In the case of from_slice(), you only need to specify the success value type. So all you need is from_slice::<Value>(buf).

match serde_json::from_slice::<serde_json::Value>(buf) {
    Ok(_i) => (),
    Err(_e) => (),
};