finishing work

This commit is contained in:
Aymeric Larvet 2021-05-09 20:17:15 +02:00
parent cd74afcb3c
commit 82939cb7a4
12 changed files with 43 additions and 36 deletions

View file

@ -11,15 +11,17 @@
// Execute the command `rustlings hint collections3` if you need
// hints.
// I AM NOT DONE
use std::collections::HashMap;
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 :)
basket.insert(String::from("banana"), 2);
basket.insert(String::from("kiwi"), 7);
basket.insert(String::from("orange"), 4);
basket.insert(String::from("apple"), 1);
// TODO: Put more fruits in your basket here.

View file

@ -12,7 +12,6 @@
// Execute the command `rustlings hint collections4` if you need
// hints.
// I AM NOT DONE
use std::collections::HashMap;
@ -38,6 +37,10 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
// TODO: Put new fruits if not already present. Note that you
// are not allowed to put any type of fruit that's already
// present!
if !basket.contains_key(&fruit) {
basket.insert(fruit, 4);
}
}
}

View file

@ -4,11 +4,10 @@
// Make me compile and pass the test!
// Execute the command `rustlings hint collections1` if you need hints.
// I AM NOT DONE
fn array_and_vec() -> ([i32; 4], Vec<i32>) {
let a = [10, 20, 30, 40]; // a plain array
let v = // TODO: declare your vector here with the macro for vectors
let v = a.to_vec();
(a, v)
}

View file

@ -7,12 +7,10 @@
// Execute the command `rustlings hint collections2` if you need
// hints.
// I AM NOT DONE
fn vec_loop(mut v: Vec<i32>) -> Vec<i32> {
for i in v.iter_mut() {
// TODO: Fill this up so that each element in the Vec `v` is
// multiplied by 2.
*i *= 2 ;
}
// At this point, `v` should be equal to [4, 8, 12, 16, 20].

View file

@ -1,11 +1,14 @@
// enums1.rs
// Make me compile! Execute `rustlings hint enums1` for hints!
// I AM NOT DONE
#[derive(Debug)]
enum Message {
// TODO: define a few types of messages as used below
Quit,
Echo,
Move,
ChangeColor,
}
fn main() {

View file

@ -1,11 +1,12 @@
// enums2.rs
// Make me compile! Execute `rustlings hint enums2` for hints!
// I AM NOT DONE
#[derive(Debug)]
enum Message {
// TODO: define the different variants used below
Quit,
Move { x: i32, y: i32 },
Echo(String),
ChangeColor(i32, i32, i32),
}
impl Message {

View file

@ -1,10 +1,11 @@
// enums3.rs
// Address all the TODOs to make the tests pass!
// I AM NOT DONE
enum Message {
// TODO: implement the message variant types based on their usage below
Quit,
Move(Point),
Echo(String),
ChangeColor((u8, u8, u8)),
}
struct Point {
@ -36,7 +37,12 @@ impl State {
}
fn process(&mut self, message: Message) {
// TODO: create a match expression to process the different message variants
match message {
Message::Quit => self.quit(),
Message::Move(Point{x, y}) => self.move_position(Point{x, y}),
Message::ChangeColor((r, g, b)) => self.change_color((r, g, b)),
Message::Echo(s) => self.echo(s),
}
}
}

View file

@ -1,10 +1,9 @@
// modules1.rs
// Make me compile! Execute `rustlings hint modules1` for hints :)
// I AM NOT DONE
mod sausage_factory {
fn make_sausage() {
pub fn make_sausage() {
println!("sausage!");
}
}

View file

@ -1,11 +1,10 @@
// modules2.rs
// Make me compile! Execute `rustlings hint modules2` for hints :)
// I AM NOT DONE
mod delicious_snacks {
use self::fruits::PEAR as fruit;
use self::veggies::CUCUMBER as veggie;
pub use self::fruits::PEAR as fruit;
pub use self::veggies::CUCUMBER as veggie;
mod fruits {
pub const PEAR: &'static str = "Pear";

View file

@ -7,7 +7,6 @@
// you think each value is. That is, add either `string_slice` or `string`
// before the parentheses on each line. If you're right, it will compile!
// I AM NOT DONE
fn string_slice(arg: &str) {
println!("{}", arg);
@ -17,14 +16,14 @@ fn string(arg: String) {
}
fn main() {
???("blue");
???("red".to_string());
???(String::from("hi"));
???("rust is fun!".to_owned());
???("nice weather".into());
???(format!("Interpolation {}", "Station"));
???(&String::from("abc")[0..1]);
???(" hello there ".trim());
???("Happy Monday!".to_string().replace("Mon", "Tues"));
???("mY sHiFt KeY iS sTiCkY".to_lowercase());
string_slice("blue");
string("red".to_string());
string(String::from("hi"));
string("rust is fun!".to_owned());
string_slice("nice weather".into());
string(format!("Interpolation {}", "Station"));
string_slice(&String::from("abc")[0..1]);
string_slice(" hello there ".trim());
string("Happy Monday!".to_string().replace("Mon", "Tues"));
string("mY sHiFt KeY iS sTiCkY".to_lowercase());
}

View file

@ -2,7 +2,6 @@
// Make me compile without changing the function signature!
// Execute `rustlings hint strings1` for hints ;)
// I AM NOT DONE
fn main() {
let answer = current_favorite_color();
@ -10,5 +9,5 @@ fn main() {
}
fn current_favorite_color() -> String {
"blue"
"blue".to_string()
}

View file

@ -2,11 +2,10 @@
// Make me compile without changing the function signature!
// Execute `rustlings hint strings2` for hints :)
// I AM NOT DONE
fn main() {
let word = String::from("green"); // Try not changing this line :)
if is_a_color_word(word) {
if is_a_color_word(&word) {
println!("That is a color word I know!");
} else {
println!("That is not a color word I know.");