Skip to content

Commit ab4221a

Browse files
committed
renumber
1 parent 48b031a commit ab4221a

File tree

3 files changed

+38
-35
lines changed

3 files changed

+38
-35
lines changed

src/example20.rs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![allow(dead_code)]
2+
13
use std::collections::HashMap;
24
use std::f32::INFINITY;
35
use std::thread;
@@ -9,10 +11,10 @@ struct Store {
911

1012
impl Store {
1113
fn new(name: String) -> Store {
12-
Store {
13-
name: name,
14-
prices: HashMap::new(),
15-
}
14+
Store {
15+
name: name,
16+
prices: HashMap::new(),
17+
}
1618
}
1719

1820
fn add_item(&mut self, name: String, price: f32) {
@@ -49,27 +51,22 @@ fn build_stores() -> Vec<Store> {
4951
}
5052

5153
fn find_best_store(stores: Vec<Store>, shopping_list: &Vec<String>) -> String {
52-
let threads: Vec<_> =
53-
stores.into_iter()
54-
.map(|store| {
55-
let shopping_list = shopping_list.clone();
56-
thread::spawn(move || {
57-
let sum = compute_sum(&store, &shopping_list);
58-
(store.name, sum)
59-
})
60-
})
61-
.collect();
62-
54+
assert!(stores.len() > 0);
6355
let mut best = None;
6456
let mut best_price = INFINITY;
65-
for thread in threads {
66-
let (name, sum) = thread.join().unwrap(); // propoagate panics
57+
for store in stores {
58+
let name = store.name.clone();
59+
let shopping_list = shopping_list.clone();
60+
let handle = thread::spawn(move || {
61+
compute_sum(&store, &shopping_list)
62+
});
63+
let sum = handle.join().unwrap();
6764
if sum < best_price {
6865
best = Some(name);
6966
best_price = sum;
7067
}
7168
}
72-
best.unwrap()
69+
best.unwrap() // there will always be at least one store
7370
}
7471

7572
fn compute_sum(store: &Store, shopping_list: &Vec<String>) -> f32 {
@@ -82,7 +79,6 @@ pub fn main() {
8279
let shopping_list = vec![format!("chocolate"),
8380
format!("doll"),
8481
format!("bike")];
85-
8682
let stores = build_stores();
8783
let best_store = find_best_store(stores, &shopping_list);
8884
println!("Best store: {}", best_store);

src/example15.rs renamed to src/example20sol.rs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
#![allow(dead_code)]
2-
31
use std::collections::HashMap;
2+
use std::f32::INFINITY;
43
use std::thread;
54

65
struct Store {
@@ -10,10 +9,10 @@ struct Store {
109

1110
impl Store {
1211
fn new(name: String) -> Store {
13-
Store {
14-
name: name,
15-
prices: HashMap::new(),
16-
}
12+
Store {
13+
name: name,
14+
prices: HashMap::new(),
15+
}
1716
}
1817

1918
fn add_item(&mut self, name: String, price: f32) {
@@ -50,22 +49,27 @@ fn build_stores() -> Vec<Store> {
5049
}
5150

5251
fn find_best_store(stores: Vec<Store>, shopping_list: &Vec<String>) -> String {
53-
assert!(stores.len() > 0);
52+
let threads: Vec<_> =
53+
stores.into_iter()
54+
.map(|store| {
55+
let shopping_list = shopping_list.clone();
56+
thread::spawn(move || {
57+
let sum = compute_sum(&store, &shopping_list);
58+
(store.name, sum)
59+
})
60+
})
61+
.collect();
62+
5463
let mut best = None;
5564
let mut best_price = INFINITY;
56-
for store in stores {
57-
let name = store.name.clone();
58-
let shopping_list = shopping_list.clone();
59-
let handle = thread::spawn(move || {
60-
compute_sum(&store, &shopping_list)
61-
});
62-
let sum = handle.join().unwrap();
65+
for thread in threads {
66+
let (name, sum) = thread.join().unwrap(); // propoagate panics
6367
if sum < best_price {
64-
best = Some(store.name);
68+
best = Some(name);
6569
best_price = sum;
6670
}
6771
}
68-
best.unwrap() // there will always be at least one store
72+
best.unwrap()
6973
}
7074

7175
fn compute_sum(store: &Store, shopping_list: &Vec<String>) -> f32 {
@@ -78,6 +82,7 @@ pub fn main() {
7882
let shopping_list = vec![format!("chocolate"),
7983
format!("doll"),
8084
format!("bike")];
85+
8186
let stores = build_stores();
8287
let best_store = find_best_store(stores, &shopping_list);
8388
println!("Best store: {}", best_store);

src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod example02;
44
mod example03;
55
mod example10;
66
mod example20;
7+
mod example20sol;
78
mod example30;
89
mod example40;
910

@@ -25,6 +26,7 @@ fn main() {
2526

2627
println!("----------------------------------------------------------------------");
2728
example20::main();
29+
example20sol::main();
2830

2931
println!("----------------------------------------------------------------------");
3032
example30::main();

0 commit comments

Comments
 (0)