Skip to content

Instantly share code, notes, and snippets.

@etscrivner
Created September 17, 2014 01:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save etscrivner/fcda4a6af6af6216d363 to your computer and use it in GitHub Desktop.
Save etscrivner/fcda4a6af6af6216d363 to your computer and use it in GitHub Desktop.
Simple redis client in rust
use std::io::TcpStream;
use std::str;
fn main() {
let mut client = match TcpStream::connect("127.0.0.1", 6379) {
Ok(c) => c,
Err(e) => fail!("Failed: {}", e)
};
let command_parts = ["keys", "*"];
let mut full_command = format!("*{}\r\n", command_parts.len());
for &part in command_parts.iter() {
full_command = format!("{}${}\r\n{}\r\n", full_command, part.len(), part)
}
println!("{}", full_command);
match client.write(full_command.as_bytes()) {
Err(e) => fail!("{}", e),
_ => {}
};
let mut buf = [0u8, .. 256];
match client.read(buf) {
Err(e) => fail!("Failed to read from redis client: {}", e),
Ok(r) => r
};
match str::from_utf8(buf) {
Some(x) => println!("{}", x),
None => { }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment