访问Redis时出现静默错误 [英] Silent error while accessing Redis

查看:37
本文介绍了访问Redis时出现静默错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Rust 的新手.我正在使用板条箱 redis = "0.3.1" 但程序只是退出而不会引起恐慌.我做的唯一不同的是数据库不同.

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

推荐答案

简短回答

出现错误,但您忽略了它.

Short answer

The error is raised, but you are ignoring it.

非致命错误通常通过返回 Result 来传播,因此调用者可以处理错误.恐慌主要用于不可恢复的错误,并将中止当前线程.在这种情况下,redis 库使用了 RedisResult 类型,它是 Result 的别名.

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

另见:错误处理(The Rust Book)

这篇关于访问Redis时出现静默错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆