-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathclosures_count_selected_cities.rs
More file actions
40 lines (35 loc) · 1.01 KB
/
closures_count_selected_cities.rs
File metadata and controls
40 lines (35 loc) · 1.01 KB
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
// Closures don't have function types.
struct City {
name: String,
population: i64,
country: String,
monster_attack_risk: f64
}
fn main() {
struct Prefs;
impl Prefs {
fn acceptable_monster_risk(&self) -> f64 { 0.0 }
}
let my_cities: Vec<City> = vec![];
let preferences = Prefs;
/// Given a list of cities and a test function,
/// return how many cities pass the test.
fn count_selected_cities(cities: &Vec<City>,
test_fn: fn(&City) -> bool) -> usize
{
let mut count = 0;
for city in cities {
if test_fn(city) {
count += 1;
}
}
count
}
let limit = preferences.acceptable_monster_risk();
let n = count_selected_cities(
&my_cities,
|city| city.monster_attack_risk > limit); // error: type mismatch
//~^ ERROR: mismatched types
//~| NOTE: expected fn pointer, found closure
//~| NOTE: expected type `for<'r> fn(&'r City) -> bool`
}