finishing work
This commit is contained in:
parent
cd74afcb3c
commit
82939cb7a4
exercises
|
@ -11,15 +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"), 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.
|
// TODO: Put more fruits in your basket here.
|
||||||
|
|
||||||
|
|
|
@ -12,7 +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;
|
||||||
|
|
||||||
|
@ -38,6 +37,10 @@ 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!
|
||||||
|
if !basket.contains_key(&fruit) {
|
||||||
|
basket.insert(fruit, 4);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,11 +4,10 @@
|
||||||
// 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 = a.to_vec();
|
||||||
|
|
||||||
(a, v)
|
(a, v)
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,12 +7,10 @@
|
||||||
// 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,11 +1,14 @@
|
||||||
// enums1.rs
|
// enums1.rs
|
||||||
// Make me compile! Execute `rustlings hint enums1` for hints!
|
// Make me compile! Execute `rustlings hint enums1` for hints!
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum Message {
|
enum Message {
|
||||||
// TODO: define a few types of messages as used below
|
// TODO: define a few types of messages as used below
|
||||||
|
Quit,
|
||||||
|
Echo,
|
||||||
|
Move,
|
||||||
|
ChangeColor,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
// enums2.rs
|
// enums2.rs
|
||||||
// Make me compile! Execute `rustlings hint enums2` for hints!
|
// Make me compile! Execute `rustlings hint enums2` for hints!
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum Message {
|
enum Message {
|
||||||
// TODO: define the different variants used below
|
Quit,
|
||||||
|
Move { x: i32, y: i32 },
|
||||||
|
Echo(String),
|
||||||
|
ChangeColor(i32, i32, i32),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Message {
|
impl Message {
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
// enums3.rs
|
// enums3.rs
|
||||||
// Address all the TODOs to make the tests pass!
|
// Address all the TODOs to make the tests pass!
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
enum Message {
|
enum Message {
|
||||||
// TODO: implement the message variant types based on their usage below
|
Quit,
|
||||||
|
Move(Point),
|
||||||
|
Echo(String),
|
||||||
|
ChangeColor((u8, u8, u8)),
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Point {
|
struct Point {
|
||||||
|
@ -36,7 +37,12 @@ impl State {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn process(&mut self, message: Message) {
|
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),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
// 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,10 @@
|
||||||
// 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";
|
||||||
|
|
|
@ -7,7 +7,6 @@
|
||||||
// you think each value is. That is, add either `string_slice` or `string`
|
// 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!
|
// before the parentheses on each line. If you're right, it will compile!
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn string_slice(arg: &str) {
|
fn string_slice(arg: &str) {
|
||||||
println!("{}", arg);
|
println!("{}", arg);
|
||||||
|
@ -17,14 +16,14 @@ fn string(arg: String) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
???("blue");
|
string_slice("blue");
|
||||||
???("red".to_string());
|
string("red".to_string());
|
||||||
???(String::from("hi"));
|
string(String::from("hi"));
|
||||||
???("rust is fun!".to_owned());
|
string("rust is fun!".to_owned());
|
||||||
???("nice weather".into());
|
string_slice("nice weather".into());
|
||||||
???(format!("Interpolation {}", "Station"));
|
string(format!("Interpolation {}", "Station"));
|
||||||
???(&String::from("abc")[0..1]);
|
string_slice(&String::from("abc")[0..1]);
|
||||||
???(" hello there ".trim());
|
string_slice(" hello there ".trim());
|
||||||
???("Happy Monday!".to_string().replace("Mon", "Tues"));
|
string("Happy Monday!".to_string().replace("Mon", "Tues"));
|
||||||
???("mY sHiFt KeY iS sTiCkY".to_lowercase());
|
string("mY sHiFt KeY iS sTiCkY".to_lowercase());
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
// Make me compile without changing the function signature!
|
// Make me compile without changing the function signature!
|
||||||
// Execute `rustlings hint strings1` for hints ;)
|
// Execute `rustlings hint strings1` for hints ;)
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let answer = current_favorite_color();
|
let answer = current_favorite_color();
|
||||||
|
@ -10,5 +9,5 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn current_favorite_color() -> String {
|
fn current_favorite_color() -> String {
|
||||||
"blue"
|
"blue".to_string()
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,11 +2,10 @@
|
||||||
// Make me compile without changing the function signature!
|
// Make me compile without changing the function signature!
|
||||||
// Execute `rustlings hint strings2` for hints :)
|
// Execute `rustlings hint strings2` for hints :)
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let word = String::from("green"); // Try not changing this line :)
|
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!");
|
println!("That is a color word I know!");
|
||||||
} else {
|
} else {
|
||||||
println!("That is not a color word I know.");
|
println!("That is not a color word I know.");
|
||||||
|
|
Loading…
Reference in a new issue