Silent error while accessing Redis

主宰稳场 提交于 2020-01-03 17:08:09

问题


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(())
}

回答1:


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)



来源:https://stackoverflow.com/questions/31084967/silent-error-while-accessing-redis

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!