Silent error while accessing Redis

2019-06-25 23:17发布

I am a newbie with Rust. I am using the crate redis = "0.3.1" but the program simply exits without raising a panic. The only thing I am doing different is that the database is different.

extern crate redis;

use redis::*;
use std::string::String;
use std::collections::HashSet;

fn main() {
    read_meta_keys_redis("myset".to_string());
}

fn read_meta_keys_redis(key: String) -> redis::RedisResult<()> {
    println!("22{}", key);
    let client = try!(redis::Client::open("redis://127.0.0.1:6379/2"));

    let con = try!(client.get_connection());
    let mems: HashSet<i32> = try!(con.smembers(key));
    for x in mems.iter() {
        println!("op-->{}", x);
    }
    Ok(())
}

标签: redis rust
1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-06-25 23:37

Short answer

The error is raised, but you are ignoring it.

Long anser

Non-fatal errors are usually propagated by returning a Result, so the caller can handle the error. Panics are mostly used for unrecoverable errors and will abort the current thread. In this case, the redis library uses the RedisResult type, which is an alias for Result<T, RedisError>.

If you want to handle the error, you should do so by matching on the result type. Try changing your main function to the following:

fn main() {
    if let Err(e) = read_meta_keys_redis("myset".to_string()) {
        println!("{}", e.description());
    }
}

See also: Error Handling (The Rust Book)

查看更多
登录 后发表回答