Modules and collections exercises complete
This commit is contained in:
parent
c328bd6f5e
commit
c59e85df74
exercises
|
@ -11,17 +11,17 @@
|
||||||
// Execute the command `rustlings hint collections3` if you need
|
// Execute the command `rustlings hint collections3` if you need
|
||||||
// hints.
|
// hints.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
fn fruit_basket() -> HashMap<String, u32> {
|
fn fruit_basket() -> HashMap<String, u32> {
|
||||||
let mut basket = // TODO: declare your hash map here.
|
let mut basket: HashMap<String, u32> = HashMap::new(); // TODO: declare your hash map here.
|
||||||
|
|
||||||
// Two bananas are already given for you :)
|
// Two bananas are already given for you :)
|
||||||
basket.insert(String::from("banana"), 2);
|
basket.insert(String::from("banana"), 2);
|
||||||
|
|
||||||
// TODO: Put more fruits in your basket here.
|
// TODO: Put more fruits in your basket here.
|
||||||
|
basket.insert(String::from("orange"), 3);
|
||||||
|
basket.insert(String::from("pear"), 4);
|
||||||
|
|
||||||
basket
|
basket
|
||||||
}
|
}
|
||||||
|
@ -39,8 +39,6 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn at_least_five_fruits() {
|
fn at_least_five_fruits() {
|
||||||
let basket = fruit_basket();
|
let basket = fruit_basket();
|
||||||
assert!(basket
|
assert!(basket.values().sum::<u32>() >= 5);
|
||||||
.values()
|
|
||||||
.sum::<u32>() >= 5);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,6 +38,7 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
|
||||||
// TODO: Put new fruits if not already present. Note that you
|
// TODO: Put new fruits if not already present. Note that you
|
||||||
// are not allowed to put any type of fruit that's already
|
// are not allowed to put any type of fruit that's already
|
||||||
// present!
|
// present!
|
||||||
|
basket.entry(fruit).or_insert(19);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,9 +76,7 @@ mod tests {
|
||||||
fn greater_than_eleven_fruits() {
|
fn greater_than_eleven_fruits() {
|
||||||
let mut basket = get_fruit_basket();
|
let mut basket = get_fruit_basket();
|
||||||
fruit_basket(&mut basket);
|
fruit_basket(&mut basket);
|
||||||
let count = basket
|
let count = basket.values().sum::<u32>();
|
||||||
.values()
|
|
||||||
.sum::<u32>();
|
|
||||||
assert!(count > 11);
|
assert!(count > 11);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,11 +4,9 @@
|
||||||
// Make me compile and pass the test!
|
// Make me compile and pass the test!
|
||||||
// Execute the command `rustlings hint collections1` if you need hints.
|
// Execute the command `rustlings hint collections1` if you need hints.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn array_and_vec() -> ([i32; 4], Vec<i32>) {
|
fn array_and_vec() -> ([i32; 4], Vec<i32>) {
|
||||||
let a = [10, 20, 30, 40]; // a plain array
|
let a = [10, 20, 30, 40]; // a plain array
|
||||||
let v = // TODO: declare your vector here with the macro for vectors
|
let v = vec![10, 20, 30, 40]; // TODO: declare your vector here with the macro for vectors
|
||||||
|
|
||||||
(a, v)
|
(a, v)
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,12 +7,11 @@
|
||||||
// Execute the command `rustlings hint collections2` if you need
|
// Execute the command `rustlings hint collections2` if you need
|
||||||
// hints.
|
// hints.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn vec_loop(mut v: Vec<i32>) -> Vec<i32> {
|
fn vec_loop(mut v: Vec<i32>) -> Vec<i32> {
|
||||||
for i in v.iter_mut() {
|
for i in v.iter_mut() {
|
||||||
// TODO: Fill this up so that each element in the Vec `v` is
|
// TODO: Fill this up so that each element in the Vec `v` is
|
||||||
// multiplied by 2.
|
// multiplied by 2.
|
||||||
|
*i *= 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
// At this point, `v` should be equal to [4, 8, 12, 16, 20].
|
// At this point, `v` should be equal to [4, 8, 12, 16, 20].
|
||||||
|
@ -28,11 +27,6 @@ mod tests {
|
||||||
let v: Vec<i32> = (1..).filter(|x| x % 2 == 0).take(5).collect();
|
let v: Vec<i32> = (1..).filter(|x| x % 2 == 0).take(5).collect();
|
||||||
let ans = vec_loop(v.clone());
|
let ans = vec_loop(v.clone());
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(ans, v.iter().map(|x| x * 2).collect::<Vec<i32>>());
|
||||||
ans,
|
|
||||||
v.iter()
|
|
||||||
.map(|x| x * 2)
|
|
||||||
.collect::<Vec<i32>>()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,8 @@
|
||||||
// modules1.rs
|
// modules1.rs
|
||||||
// Make me compile! Execute `rustlings hint modules1` for hints :)
|
// Make me compile! Execute `rustlings hint modules1` for hints :)
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
mod sausage_factory {
|
mod sausage_factory {
|
||||||
fn make_sausage() {
|
pub fn make_sausage() {
|
||||||
println!("sausage!");
|
println!("sausage!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,9 @@
|
||||||
// modules2.rs
|
// modules2.rs
|
||||||
// Make me compile! Execute `rustlings hint modules2` for hints :)
|
// Make me compile! Execute `rustlings hint modules2` for hints :)
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
mod delicious_snacks {
|
mod delicious_snacks {
|
||||||
use self::fruits::PEAR as fruit;
|
pub use self::fruits::PEAR as fruit;
|
||||||
use self::veggies::CUCUMBER as veggie;
|
pub use self::veggies::CUCUMBER as veggie;
|
||||||
|
|
||||||
mod fruits {
|
mod fruits {
|
||||||
pub const PEAR: &'static str = "Pear";
|
pub const PEAR: &'static str = "Pear";
|
||||||
|
|
Loading…
Reference in a new issue