Get through quiz 2

This commit is contained in:
Carson Rajcan 2021-01-17 20:59:29 -06:00
parent cda45ceec1
commit 70024e1b6b
21 changed files with 64 additions and 88 deletions

View file

@ -11,17 +11,16 @@
// 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();// 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);
basket.insert(String::from("apple"), 2);
basket.insert(String::from("cherry"), 3);
// TODO: Put more fruits in your basket here.
basket basket
} }

View file

@ -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,7 @@ 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 basket.entry(fruit).or_insert(1);
// are not allowed to put any type of fruit that's already
// present!
} }
} }

View file

@ -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 = a.to_vec(); // TODO: declare your vector here with the macro for vectors
(a, v) (a, v)
} }

View file

@ -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 = *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].

View file

@ -1,11 +1,12 @@
// 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 Quit,
Echo,
Move,
ChangeColor
} }
fn main() { fn main() {

View file

@ -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 Move { x: i32, y: i32 },
Echo(String),
Quit,
ChangeColor(i32, i32, i32)
} }
impl Message { impl Message {

View file

@ -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 ChangeColor((u8, u8, u8)),
Echo(String),
Move(Point),
Quit
} }
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::ChangeColor(color) => self.change_color(color),
Message::Echo(comment) => self.echo(comment),
Message::Move(point) => self.move_position(point),
Message::Quit => self.quit()
}
} }
} }

View file

@ -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!");
} }
} }

View file

@ -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";

View file

@ -2,8 +2,6 @@
// Fill in the rest of the line that has code missing! // Fill in the rest of the line that has code missing!
// No hints, there's no tricks, just get used to typing these :) // No hints, there's no tricks, just get used to typing these :)
// I AM NOT DONE
fn main() { fn main() {
// Booleans (`bool`) // Booleans (`bool`)
@ -12,7 +10,7 @@ fn main() {
println!("Good morning!"); println!("Good morning!");
} }
let // Finish the rest of this line like the example! Or make it be false! let is_evening = false;// Finish the rest of this line like the example! Or make it be false!
if is_evening { if is_evening {
println!("Good evening!"); println!("Good evening!");
} }

View file

@ -2,8 +2,6 @@
// Fill in the rest of the line that has code missing! // Fill in the rest of the line that has code missing!
// No hints, there's no tricks, just get used to typing these :) // No hints, there's no tricks, just get used to typing these :)
// I AM NOT DONE
fn main() { fn main() {
// Characters (`char`) // Characters (`char`)
@ -16,7 +14,7 @@ fn main() {
println!("Neither alphabetic nor numeric!"); println!("Neither alphabetic nor numeric!");
} }
let // Finish this line like the example! What's your favorite character? let your_character = '&';// Finish this line like the example! What's your favorite character?
// Try a letter, try a number, try a special character, try a character // Try a letter, try a number, try a special character, try a character
// from a different language than your own, try an emoji! // from a different language than your own, try an emoji!
if your_character.is_alphabetic() { if your_character.is_alphabetic() {

View file

@ -2,10 +2,8 @@
// Create an array with at least 100 elements in it where the ??? is. // Create an array with at least 100 elements in it where the ??? is.
// Execute `rustlings hint primitive_types3` for hints! // Execute `rustlings hint primitive_types3` for hints!
// I AM NOT DONE
fn main() { fn main() {
let a = ??? let a = ["cool"; 101];
if a.len() >= 100 { if a.len() >= 100 {
println!("Wow, that's a big array!"); println!("Wow, that's a big array!");

View file

@ -2,13 +2,11 @@
// Get a slice out of Array a where the ??? is so that the test passes. // Get a slice out of Array a where the ??? is so that the test passes.
// Execute `rustlings hint primitive_types4` for hints!! // Execute `rustlings hint primitive_types4` for hints!!
// I AM NOT DONE
#[test] #[test]
fn slice_out_of_array() { fn slice_out_of_array() {
let a = [1, 2, 3, 4, 5]; let a = [1, 2, 3, 4, 5];
let nice_slice = ??? let nice_slice = &a[1..4];
assert_eq!([2, 3, 4], nice_slice) assert_eq!([2, 3, 4], nice_slice)
} }

View file

@ -2,11 +2,9 @@
// Destructure the `cat` tuple so that the println will work. // Destructure the `cat` tuple so that the println will work.
// Execute `rustlings hint primitive_types5` for hints! // Execute `rustlings hint primitive_types5` for hints!
// I AM NOT DONE
fn main() { fn main() {
let cat = ("Furry McFurson", 3.5); let cat = ("Furry McFurson", 3.5);
let /* your pattern here */ = cat; let (name, age) = cat;
println!("{} is {} years old.", name, age); println!("{} is {} years old.", name, age);
} }

View file

@ -3,13 +3,11 @@
// You can put the expression for the second element where ??? is so that the test passes. // You can put the expression for the second element where ??? is so that the test passes.
// Execute `rustlings hint primitive_types6` for hints! // Execute `rustlings hint primitive_types6` for hints!
// I AM NOT DONE
#[test] #[test]
fn indexing_tuple() { fn indexing_tuple() {
let numbers = (1, 2, 3); let numbers = (1, 2, 3);
// Replace below ??? with the tuple indexing syntax. // Replace below ??? with the tuple indexing syntax.
let second = ???; let second = numbers.1;
assert_eq!(2, second, assert_eq!(2, second,
"This is not the 2nd number in the tuple!") "This is not the 2nd number in the tuple!")

View file

@ -7,8 +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 +15,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("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());
} }

View file

@ -2,13 +2,11 @@
// 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();
println!("My current favorite color is {}", answer); println!("My current favorite color is {}", answer);
} }
fn current_favorite_color() -> String { fn current_favorite_color() -> String {
"blue" "blue".into()
} }

View file

@ -2,11 +2,9 @@
// 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.");

View file

@ -1,13 +1,12 @@
// structs1.rs // structs1.rs
// Address all the TODOs to make the tests pass! // Address all the TODOs to make the tests pass!
// I AM NOT DONE struct ColorClassicStruct<'a> {
name: &'a str,
struct ColorClassicStruct { hex: &'a str
// TODO: Something goes here
} }
struct ColorTupleStruct(/* TODO: Something goes here */); struct ColorTupleStruct<'a>(&'a str, &'a str);
#[derive(Debug)] #[derive(Debug)]
struct UnitStruct; struct UnitStruct;
@ -19,7 +18,7 @@ mod tests {
#[test] #[test]
fn classic_c_structs() { fn classic_c_structs() {
// TODO: Instantiate a classic c struct! // TODO: Instantiate a classic c struct!
// let green = let green = ColorClassicStruct { name: "green", hex: "#00FF00" };
assert_eq!(green.name, "green"); assert_eq!(green.name, "green");
assert_eq!(green.hex, "#00FF00"); assert_eq!(green.hex, "#00FF00");
@ -28,7 +27,7 @@ mod tests {
#[test] #[test]
fn tuple_structs() { fn tuple_structs() {
// TODO: Instantiate a tuple struct! // TODO: Instantiate a tuple struct!
// let green = let green = ColorTupleStruct("green", "#00FF00");
assert_eq!(green.0, "green"); assert_eq!(green.0, "green");
assert_eq!(green.1, "#00FF00"); assert_eq!(green.1, "#00FF00");
@ -37,7 +36,7 @@ mod tests {
#[test] #[test]
fn unit_structs() { fn unit_structs() {
// TODO: Instantiate a unit struct! // TODO: Instantiate a unit struct!
// let unit_struct = let unit_struct = UnitStruct;
let message = format!("{:?}s are fun!", unit_struct); let message = format!("{:?}s are fun!", unit_struct);
assert_eq!(message, "UnitStructs are fun!"); assert_eq!(message, "UnitStructs are fun!");

View file

@ -1,8 +1,6 @@
// structs2.rs // structs2.rs
// Address all the TODOs to make the tests pass! // Address all the TODOs to make the tests pass!
// I AM NOT DONE
#[derive(Debug)] #[derive(Debug)]
struct Order { struct Order {
name: String, name: String,
@ -34,7 +32,12 @@ mod tests {
fn your_order() { fn your_order() {
let order_template = create_order_template(); let order_template = create_order_template();
// TODO: Create your own order using the update syntax and template above! // TODO: Create your own order using the update syntax and template above!
// let your_order =
let your_order = Order {
name: "Hacker in Rust".into(),
count: order_template.count + 1,
..order_template
};
assert_eq!(your_order.name, "Hacker in Rust"); assert_eq!(your_order.name, "Hacker in Rust");
assert_eq!(your_order.year, order_template.year); assert_eq!(your_order.year, order_template.year);
assert_eq!(your_order.made_by_phone, order_template.made_by_phone); assert_eq!(your_order.made_by_phone, order_template.made_by_phone);

View file

@ -3,8 +3,6 @@
// exercise we have defined the Package struct and we want to test some logic attached to it, // exercise we have defined the Package struct and we want to test some logic attached to it,
// make the code compile and the tests pass! If you have issues execute `rustlings hint structs3` // make the code compile and the tests pass! If you have issues execute `rustlings hint structs3`
// I AM NOT DONE
#[derive(Debug)] #[derive(Debug)]
struct Package { struct Package {
sender_country: String, sender_country: String,
@ -15,7 +13,7 @@ struct Package {
impl Package { impl Package {
fn new(sender_country: String, recipient_country: String, weight_in_grams: i32) -> Package { fn new(sender_country: String, recipient_country: String, weight_in_grams: i32) -> Package {
if weight_in_grams <= 0 { if weight_in_grams <= 0 {
// Something goes here... panic!("package must have a weight")
} else { } else {
return Package { return Package {
sender_country, sender_country,
@ -25,12 +23,12 @@ impl Package {
} }
} }
fn is_international(&self) -> ??? { fn is_international(&self) -> bool {
// Something goes here... self.sender_country != self.recipient_country
} }
fn get_fees(&self, cents_per_gram: i32) -> ??? { fn get_fees(&self, cents_per_gram: i32) -> i32 {
// Something goes here... self.weight_in_grams * cents_per_gram
} }
} }
@ -62,7 +60,7 @@ mod tests {
let sender_country = String::from("Spain"); let sender_country = String::from("Spain");
let recipient_country = String::from("Spain"); let recipient_country = String::from("Spain");
let cents_per_gram = ???; let cents_per_gram = 3;
let package = Package::new(sender_country, recipient_country, 1500); let package = Package::new(sender_country, recipient_country, 1500);