Skip to content

Commit 65ff71c

Browse files
authored
prefer slices over borrowed vecs in exercise examples (#1019)
1 parent 0711c3a commit 65ff71c

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

exercises/book-store/example.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use std::hash::{Hash, Hasher};
66
use std::mem;
77

88
type Book = u32;
9-
type GroupedBasket = Vec<Group>;
109
type Price = u32;
1110
const BOOK_PRICE: Price = 800;
1211

@@ -79,16 +78,15 @@ impl Hash for Group {
7978
}
8079
}
8180

82-
fn basket_price(basket: &GroupedBasket) -> Price {
81+
fn basket_price(basket: &[Group]) -> Price {
8382
basket.iter().map(|g| g.price()).sum()
8483
}
8584

86-
/// Compute the hash of a GroupedBasket
85+
/// Compute the hash of a Vec<Group>
8786
///
8887
/// Note that we don't actually care at all about the _values_ within
89-
/// the groups, only their lengths. Therefore, let's hash not the actual
90-
/// GB but its lengths.
91-
fn hash_of(basket: &GroupedBasket) -> u64 {
88+
/// the groups, only their lengths. Therefore, let's hash only those.
89+
fn hash_of(basket: &[Group]) -> u64 {
9290
let lengths = basket
9391
.iter()
9492
.map(|g| g.0.borrow().len())
@@ -107,11 +105,11 @@ pub fn lowest_price(books: &[Book]) -> Price {
107105

108106
struct DecomposeGroups {
109107
prev_states: HashSet<u64>,
110-
next: Option<GroupedBasket>,
108+
next: Option<Vec<Group>>,
111109
}
112110

113111
impl Iterator for DecomposeGroups {
114-
type Item = GroupedBasket;
112+
type Item = Vec<Group>;
115113
fn next(&mut self) -> Option<Self::Item> {
116114
// our goal here: produce a stream of valid groups, differentiated by their
117115
// counts, from most compact to most dispersed.
@@ -168,7 +166,7 @@ impl Iterator for DecomposeGroups {
168166

169167
impl DecomposeGroups {
170168
fn new(books: &[Book]) -> DecomposeGroups {
171-
let mut book_groups = GroupedBasket::new();
169+
let mut book_groups = Vec::new();
172170
'nextbook: for book in books {
173171
for Group(book_group) in book_groups.iter() {
174172
if !book_group.borrow().contains(&book) {

0 commit comments

Comments
 (0)