ajout des collections
This commit is contained in:
parent
cb82bbc057
commit
96dd044f2b
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::new();
|
||||||
|
|
||||||
// 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"), 1);
|
||||||
|
basket.insert(String::from("tomato"), 2);
|
||||||
// TODO: Put more fruits in your basket here.
|
basket.insert(String::from("dog"), 3);
|
||||||
|
basket.insert(String::from("stuff"), 4);
|
||||||
|
basket.insert(String::from("andstuff"), 5);
|
||||||
|
|
||||||
basket
|
basket
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,8 +12,6 @@
|
||||||
// Execute the command `rustlings hint collections4` if you need
|
// Execute the command `rustlings hint collections4` if you need
|
||||||
// hints.
|
// hints.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
#[derive(Hash, PartialEq, Eq)]
|
#[derive(Hash, PartialEq, Eq)]
|
||||||
|
@ -35,9 +33,12 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
|
||||||
];
|
];
|
||||||
|
|
||||||
for fruit in fruit_kinds {
|
for fruit in fruit_kinds {
|
||||||
// TODO: Put new fruits if not already present. Note that you
|
// if !basket.contains_key(&fruit) {
|
||||||
// are not allowed to put any type of fruit that's already
|
// basket.insert(fruit, 0);
|
||||||
// present!
|
// }
|
||||||
|
|
||||||
|
// basket.insert(fruit, basket[fruit] + 1);
|
||||||
|
*basket.entry(fruit).or_insert(12) + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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];
|
||||||
|
|
||||||
(a, v)
|
(a, v)
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,12 +7,9 @@
|
||||||
// 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
|
*i *= 2;
|
||||||
// multiplied by 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].
|
||||||
|
|
|
@ -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