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
#![feature(duration_as_u128)]
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::{Duration, SystemTime};
use std::sync::mpsc;
use std::thread::JoinHandle;

fn main() {
let (tx, rx) = mpsc::channel();

let now = SystemTime::now();
let handle = thread::spawn(move || {
{
// 定时器
let tx = tx.clone();
thread::spawn(move || {// do something
thread::sleep(Duration::from_millis(500));
tx.send(Err(())).unwrap();
});
}
// 干一些不为人知的事情
thread::sleep(Duration::from_millis(300));
tx.send(Ok("result")).unwrap();
});

match rx.recv().unwrap() {
// 计时器
Ok(data) => println!("{:?}, {}", data, now.elapsed().unwrap().as_millis()),
Err(err) => {
handle.join();
}
}
}