From a88cec24893fea4c4863713d05a3fcd7142e3b12 Mon Sep 17 00:00:00 2001 From: Peter Goodspeed-Niklaus Date: Wed, 18 Nov 2020 12:31:59 +0100 Subject: [PATCH] prefer slices over borrowed vecs in exercise examples --- exercises/book-store/example.rs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/exercises/book-store/example.rs b/exercises/book-store/example.rs index c62b2b97f..dd348f96f 100644 --- a/exercises/book-store/example.rs +++ b/exercises/book-store/example.rs @@ -6,7 +6,6 @@ use std::hash::{Hash, Hasher}; use std::mem; type Book = u32; -type GroupedBasket = Vec; type Price = u32; const BOOK_PRICE: Price = 800; @@ -79,16 +78,15 @@ impl Hash for Group { } } -fn basket_price(basket: &GroupedBasket) -> Price { +fn basket_price(basket: &[Group]) -> Price { basket.iter().map(|g| g.price()).sum() } -/// Compute the hash of a GroupedBasket +/// Compute the hash of a Vec /// /// Note that we don't actually care at all about the _values_ within -/// the groups, only their lengths. Therefore, let's hash not the actual -/// GB but its lengths. -fn hash_of(basket: &GroupedBasket) -> u64 { +/// the groups, only their lengths. Therefore, let's hash only those. +fn hash_of(basket: &[Group]) -> u64 { let lengths = basket .iter() .map(|g| g.0.borrow().len()) @@ -107,11 +105,11 @@ pub fn lowest_price(books: &[Book]) -> Price { struct DecomposeGroups { prev_states: HashSet, - next: Option, + next: Option>, } impl Iterator for DecomposeGroups { - type Item = GroupedBasket; + type Item = Vec; fn next(&mut self) -> Option { // our goal here: produce a stream of valid groups, differentiated by their // counts, from most compact to most dispersed. @@ -168,7 +166,7 @@ impl Iterator for DecomposeGroups { impl DecomposeGroups { fn new(books: &[Book]) -> DecomposeGroups { - let mut book_groups = GroupedBasket::new(); + let mut book_groups = Vec::new(); 'nextbook: for book in books { for Group(book_group) in book_groups.iter() { if !book_group.borrow().contains(&book) {