scorix
scorix

Reputation: 2516

How can I remove `let _ : () = ...`?

Here is my code, and this works:

extern crate redis;

// connect to redis
fn establish_connection() -> redis::Connection {
    let client = redis::Client::open("redis://ip:port/0").unwrap();
    client.get_connection().unwrap()
}

fn main() {
    let con : redis::Connection = establish_connection();
    let _ : () = redis::cmd("RANDOMKEY").query(&con).unwrap();
}

But this one doesn't work:

//...

fn main() {
    let con : redis::Connection = establish_connection();
    redis::cmd("RANDOMKEY").query(&con).unwrap();
}

It raises an error while compiling:

error: unable to infer enough type information about `_`; type annotations or generic parameter binding required [E0282]
    redis::cmd("FLUSHALL").query(&con).unwrap();
                           ^~~~~
help: run `rustc --explain E0282` to see a detailed explanation

Do I have to write let _ : () = ...? How can I remove it?

Upvotes: 2

Views: 190

Answers (1)

Dogbert
Dogbert

Reputation: 222358

redis::Cmd::query is defined as:

fn query<T: FromRedisValue>(&self, con: &ConnectionLike) -> RedisResult<T>

You just need to make the T here () to get the same behaviour as annotating the value of query(...).unwrap() to be T. This should work:

redis::cmd("RANDOMKEY").query::<()>(&con).unwrap();

Upvotes: 4

Related Questions