mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-06-24 21:19:42 +00:00
add rust lang to repo by add assets/mini-redis
This commit is contained in:
4
zh-CN/assets/mini-redis/examples/chat.rs
Normal file
4
zh-CN/assets/mini-redis/examples/chat.rs
Normal file
@ -0,0 +1,4 @@
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
unimplemented!();
|
||||
}
|
32
zh-CN/assets/mini-redis/examples/hello_world.rs
Normal file
32
zh-CN/assets/mini-redis/examples/hello_world.rs
Normal file
@ -0,0 +1,32 @@
|
||||
//! Hello world server.
|
||||
//!
|
||||
//! A simple client that connects to a mini-redis server, sets key "hello" with value "world",
|
||||
//! and gets it from the server after
|
||||
//!
|
||||
//! You can test this out by running:
|
||||
//!
|
||||
//! cargo run --bin mini-redis-server
|
||||
//!
|
||||
//! And then in another terminal run:
|
||||
//!
|
||||
//! cargo run --example hello_world
|
||||
|
||||
#![warn(rust_2018_idioms)]
|
||||
|
||||
use mini_redis::{client, Result};
|
||||
|
||||
#[tokio::main]
|
||||
pub async fn main() -> Result<()> {
|
||||
// Open a connection to the mini-redis address.
|
||||
let mut client = client::connect("127.0.0.1:6379").await?;
|
||||
|
||||
// Set the key "hello" with value "world"
|
||||
client.set("hello", "world".into()).await?;
|
||||
|
||||
// Get key "hello"
|
||||
let result = client.get("hello").await?;
|
||||
|
||||
println!("got value from the server; success={:?}", result.is_some());
|
||||
|
||||
Ok(())
|
||||
}
|
31
zh-CN/assets/mini-redis/examples/pub.rs
Normal file
31
zh-CN/assets/mini-redis/examples/pub.rs
Normal file
@ -0,0 +1,31 @@
|
||||
//! Publish to a redis channel example.
|
||||
//!
|
||||
//! A simple client that connects to a mini-redis server, and
|
||||
//! publishes a message on `foo` channel
|
||||
//!
|
||||
//! You can test this out by running:
|
||||
//!
|
||||
//! cargo run --bin mini-redis-server
|
||||
//!
|
||||
//! Then in another terminal run:
|
||||
//!
|
||||
//! cargo run --example sub
|
||||
//!
|
||||
//! And then in another terminal run:
|
||||
//!
|
||||
//! cargo run --example pub
|
||||
|
||||
#![warn(rust_2018_idioms)]
|
||||
|
||||
use mini_redis::{client, Result};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
// Open a connection to the mini-redis address.
|
||||
let mut client = client::connect("127.0.0.1:6379").await?;
|
||||
|
||||
// publish message `bar` on channel foo
|
||||
client.publish("foo", "bar".into()).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
37
zh-CN/assets/mini-redis/examples/sub.rs
Normal file
37
zh-CN/assets/mini-redis/examples/sub.rs
Normal file
@ -0,0 +1,37 @@
|
||||
//! Subscribe to a redis channel example.
|
||||
//!
|
||||
//! A simple client that connects to a mini-redis server, subscribes to "foo" and "bar" channels
|
||||
//! and awaits messages published on those channels
|
||||
//!
|
||||
//! You can test this out by running:
|
||||
//!
|
||||
//! cargo run --bin mini-redis-server
|
||||
//!
|
||||
//! Then in another terminal run:
|
||||
//!
|
||||
//! cargo run --example sub
|
||||
//!
|
||||
//! And then in another terminal run:
|
||||
//!
|
||||
//! cargo run --example pub
|
||||
|
||||
|
||||
|
||||
use mini_redis::{client, Result};
|
||||
use tokio_stream::StreamExt;
|
||||
#[tokio::main]
|
||||
pub async fn main() -> Result<()> {
|
||||
// Open a connection to the mini-redis address.
|
||||
let client = client::connect("127.0.0.1:6379").await?;
|
||||
|
||||
// subscribe to channel foo
|
||||
let mut subscriber = client.subscribe(vec!["foo".into()]).await?;
|
||||
let messages = subscriber.into_stream();
|
||||
tokio::pin!(messages);
|
||||
// await messages on channel foo
|
||||
while let Some(msg) = messages.next().await {
|
||||
println!("got = {:?}", msg);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
Reference in New Issue
Block a user