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.
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 theRedisResult
type, which is an alias forResult<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:
See also: Error Handling (The Rust Book)