-
Notifications
You must be signed in to change notification settings - Fork 13
/
closures_sort_thread.rs
45 lines (36 loc) · 1.06 KB
/
closures_sort_thread.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
39
40
41
42
43
44
// You can't just fire off a thread that operates on a data structure stored in
// a local argument binding. That would be wildly unsafe.
// error-pattern: closure may outlive the current function, but it borrows `stat`, which is owned by the current function
struct City {
name: String,
population: i64,
country: String,
monster_attack_risk: f64
}
#[derive(Clone, Copy, PartialEq, Eq)]
enum Statistic {
Population,
MonsterAttackRisk
}
impl City {
fn get_statistic(&self, stat: Statistic) -> i64 {
match stat {
Statistic::Population => self.population,
Statistic::MonsterAttackRisk => self.monster_attack_risk as i64
}
}
}
use std::thread;
fn start_sorting_thread(mut cities: Vec<City>, stat: Statistic)
-> thread::JoinHandle<Vec<City>>
{
let key_fn = |city: &City| -> i64 { -city.get_statistic(stat) };
thread::spawn(|| {
cities.sort_by_key(key_fn);
cities
})
}
fn main() {
let jh = start_sorting_thread(vec![], Statistic::MonsterAttackRisk);
jh.join().unwrap();
}