mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-11 13:19:18 +00:00
commit
71785159a0
@ -16,12 +16,24 @@
|
|||||||
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(); // 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.
|
||||||
|
// create fruit entries to add to basket
|
||||||
|
// iteratively or usion `Vec` method
|
||||||
|
let fruits = vec![
|
||||||
|
("apple".to_string(), 4),
|
||||||
|
("pear".to_string(), 1),
|
||||||
|
("peach".to_string(), 2),
|
||||||
|
];
|
||||||
|
|
||||||
|
// `extend` (or `append`) one array to the end of another
|
||||||
|
basket.extend(fruits);
|
||||||
|
|
||||||
|
// println!("{:?}", fruits);
|
||||||
|
|
||||||
basket
|
basket
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,11 +4,9 @@
|
|||||||
// Make me compile and pass the test!
|
// Make me compile and pass the test!
|
||||||
// Execute the command `rustlings hint vec1` if you need hints.
|
// Execute the command `rustlings hint vec1` 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 vec2` if you need
|
// Execute the command `rustlings hint vec2` 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 = *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].
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user