-
Notifications
You must be signed in to change notification settings - Fork 2
/
channels_feature.rs
42 lines (33 loc) · 1.03 KB
/
channels_feature.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
use std::{thread, time};
use std::future::Future;
use std::io;
use std::net::TcpListener;
use std::sync::mpsc::{channel, Receiver, RecvError, SendError};
use std::sync::mpsc;
use std::sync::mpsc::Sender;
use futures::executor::block_on;
pub fn run() {
local_channel();
}
fn local_channel() {
let (sender, receiver): (Sender<String>, Receiver<String>) = mpsc::channel();
let sender_future = send_message(sender);
let receive_future = receive_message(receiver);
block_on(sender_future);
block_on(receive_future);
}
async fn send_message(sender: Sender<String>) {
thread::sleep(time::Duration::from_secs(2));
let ack = sender.send(String::from("Hello channel"));
match ack {
Ok(()) => println!("Message sent successful"),
Err(error) => println!("{}", error.to_string()),
}
}
async fn receive_message(receiver: Receiver<String>) {
let result = receiver.recv();
match result {
Ok(v) => println!("Received: {}", v),
Err(error) => println!("{}", error.to_string()),
}
}