mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-03 09:19:18 +00:00
🎉 complete quiz2
This commit is contained in:
parent
0d4642222f
commit
7bd260da33
@ -5,13 +5,13 @@
|
|||||||
// Execute `rustlings hint structs1` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint structs1` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
struct ColorClassicStruct {
|
struct ColorClassicStruct {
|
||||||
// TODO: Something goes here
|
red: u8,
|
||||||
|
green: u8,
|
||||||
|
blue: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ColorTupleStruct(/* TODO: Something goes here */);
|
struct ColorTupleStruct(u8,u8,u8);
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct UnitLikeStruct;
|
struct UnitLikeStruct;
|
||||||
@ -22,8 +22,11 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn classic_c_structs() {
|
fn classic_c_structs() {
|
||||||
// TODO: Instantiate a classic c struct!
|
let green = ColorClassicStruct{
|
||||||
// let green =
|
red: 0,
|
||||||
|
green: 255,
|
||||||
|
blue: 0,
|
||||||
|
};
|
||||||
|
|
||||||
assert_eq!(green.red, 0);
|
assert_eq!(green.red, 0);
|
||||||
assert_eq!(green.green, 255);
|
assert_eq!(green.green, 255);
|
||||||
@ -32,8 +35,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn tuple_structs() {
|
fn tuple_structs() {
|
||||||
// TODO: Instantiate a tuple struct!
|
let green = ColorTupleStruct(0, 255, 0);
|
||||||
// let green =
|
|
||||||
|
|
||||||
assert_eq!(green.0, 0);
|
assert_eq!(green.0, 0);
|
||||||
assert_eq!(green.1, 255);
|
assert_eq!(green.1, 255);
|
||||||
@ -42,8 +44,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn unit_structs() {
|
fn unit_structs() {
|
||||||
// TODO: Instantiate a unit-like struct!
|
let unit_like_struct = UnitLikeStruct;
|
||||||
// let unit_like_struct =
|
|
||||||
let message = format!("{:?}s are fun!", unit_like_struct);
|
let message = format!("{:?}s are fun!", unit_like_struct);
|
||||||
|
|
||||||
assert_eq!(message, "UnitLikeStructs are fun!");
|
assert_eq!(message, "UnitLikeStructs are fun!");
|
||||||
|
|||||||
@ -5,8 +5,6 @@
|
|||||||
// Execute `rustlings hint structs2` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint structs2` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Order {
|
struct Order {
|
||||||
name: String,
|
name: String,
|
||||||
@ -38,7 +36,11 @@ 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: String::from("Hacker in Rust"),
|
||||||
|
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);
|
||||||
|
|||||||
@ -7,7 +7,6 @@
|
|||||||
// Execute `rustlings hint structs3` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint structs3` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Package {
|
struct Package {
|
||||||
@ -31,12 +30,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: u32) -> ??? {
|
fn get_fees(&self, cents_per_gram: u32) -> u32 {
|
||||||
// Something goes here...
|
self.weight_in_grams * cents_per_gram
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -2,11 +2,13 @@
|
|||||||
//
|
//
|
||||||
// No hints this time! ;)
|
// No hints this time! ;)
|
||||||
|
|
||||||
// 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() {
|
||||||
|
|||||||
@ -3,11 +3,12 @@
|
|||||||
// Execute `rustlings hint enums2` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint enums2` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// 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),
|
||||||
|
ChangeColor(i32, i32, i32),
|
||||||
|
Quit,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Message {
|
impl Message {
|
||||||
|
|||||||
@ -5,10 +5,11 @@
|
|||||||
// Execute `rustlings hint enums3` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint enums3` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
enum Message {
|
enum Message {
|
||||||
// TODO: implement the message variant types based on their usage below
|
Move(Point),
|
||||||
|
Echo(String),
|
||||||
|
ChangeColor(u8, u8, u8),
|
||||||
|
Quit,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Point {
|
struct Point {
|
||||||
@ -41,6 +42,13 @@ impl State {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn process(&mut self, message: Message) {
|
fn process(&mut self, message: Message) {
|
||||||
|
use Message::*;
|
||||||
|
match message {
|
||||||
|
Move(p) => self.move_position(p),
|
||||||
|
Echo(str) => self.echo(str),
|
||||||
|
ChangeColor(r,g,b) => self.change_color((r,g,b)),
|
||||||
|
Quit => self.quit()
|
||||||
|
}
|
||||||
// TODO: create a match expression to process the different message variants
|
// TODO: create a match expression to process the different message variants
|
||||||
// Remember: When passing a tuple as a function argument, you'll need extra parentheses:
|
// Remember: When passing a tuple as a function argument, you'll need extra parentheses:
|
||||||
// fn function((t, u, p, l, e))
|
// fn function((t, u, p, l, e))
|
||||||
|
|||||||
@ -5,7 +5,6 @@
|
|||||||
// Execute `rustlings hint strings1` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint strings1` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let answer = current_favorite_color();
|
let answer = current_favorite_color();
|
||||||
@ -13,5 +12,5 @@ fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn current_favorite_color() -> String {
|
fn current_favorite_color() -> String {
|
||||||
"blue"
|
String::from("blue")
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,11 +5,9 @@
|
|||||||
// Execute `rustlings hint strings2` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint strings2` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// 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.");
|
||||||
|
|||||||
@ -3,21 +3,17 @@
|
|||||||
// Execute `rustlings hint strings3` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint strings3` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn trim_me(input: &str) -> String {
|
fn trim_me(input: &str) -> String {
|
||||||
// TODO: Remove whitespace from both ends of a string!
|
String::from(input).trim().to_string()
|
||||||
???
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compose_me(input: &str) -> String {
|
fn compose_me(input: &str) -> String {
|
||||||
// TODO: Add " world!" to the string! There are multiple ways to do this!
|
String::from(input) + " world!"
|
||||||
???
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn replace_me(input: &str) -> String {
|
fn replace_me(input: &str) -> String {
|
||||||
// TODO: Replace "cars" in the string with "balloons"!
|
String::from(input).replace("cars", "balloons")
|
||||||
???
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
@ -7,7 +7,6 @@
|
|||||||
//
|
//
|
||||||
// No hints this time!
|
// No hints this time!
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn string_slice(arg: &str) {
|
fn string_slice(arg: &str) {
|
||||||
println!("{}", arg);
|
println!("{}", arg);
|
||||||
@ -17,14 +16,15 @@ 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_slice("nice weather".into());
|
||||||
???(&String::from("abc")[0..1]);
|
string(format!("Interpolation {}", "Station"));
|
||||||
???(" hello there ".trim());
|
string_slice(&String::from("abc")[0..1]);
|
||||||
???("Happy Monday!".to_string().replace("Mon", "Tues"));
|
string_slice(" hello there ".trim());
|
||||||
???("mY sHiFt KeY iS sTiCkY".to_lowercase());
|
string("Happy Monday!".to_string().replace("Mon", "Tues"));
|
||||||
|
string("mY sHiFt KeY iS sTiCkY".to_lowercase());
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,7 +3,6 @@
|
|||||||
// Execute `rustlings hint modules1` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint modules1` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
mod sausage_factory {
|
mod sausage_factory {
|
||||||
// Don't let anybody outside of this module see this!
|
// Don't let anybody outside of this module see this!
|
||||||
@ -11,7 +10,7 @@ mod sausage_factory {
|
|||||||
String::from("Ginger")
|
String::from("Ginger")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn make_sausage() {
|
pub fn make_sausage() {
|
||||||
get_secret_recipe();
|
get_secret_recipe();
|
||||||
println!("sausage!");
|
println!("sausage!");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,12 +7,11 @@
|
|||||||
// Execute `rustlings hint modules2` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint modules2` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
mod delicious_snacks {
|
mod delicious_snacks {
|
||||||
// TODO: Fix these use statements
|
// TODO: Fix these use statements
|
||||||
use self::fruits::PEAR as ???
|
pub use self::fruits::PEAR as fruit;
|
||||||
use self::veggies::CUCUMBER as ???
|
pub use self::veggies::CUCUMBER as veggie;
|
||||||
|
|
||||||
mod fruits {
|
mod fruits {
|
||||||
pub const PEAR: &'static str = "Pear";
|
pub const PEAR: &'static str = "Pear";
|
||||||
|
|||||||
BIN
exercises/10_modules/modules3
Executable file
BIN
exercises/10_modules/modules3
Executable file
Binary file not shown.
@ -8,10 +8,8 @@
|
|||||||
// Execute `rustlings hint modules3` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint modules3` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
// TODO: Complete this use statement
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
use ???
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
match SystemTime::now().duration_since(UNIX_EPOCH) {
|
match SystemTime::now().duration_since(UNIX_EPOCH) {
|
||||||
|
|||||||
@ -11,17 +11,18 @@
|
|||||||
// Execute `rustlings hint hashmaps1` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint hashmaps1` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// 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);
|
||||||
|
|
||||||
// TODO: Put more fruits in your basket here.
|
// TODO: Put more fruits in your basket here.
|
||||||
|
basket.insert(String::from("apple"), 2);
|
||||||
|
basket.insert(String::from("orange"), 1);
|
||||||
|
|
||||||
basket
|
basket
|
||||||
}
|
}
|
||||||
|
|||||||
@ -14,7 +14,6 @@
|
|||||||
// Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
@ -40,6 +39,10 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
|
|||||||
// TODO: Insert new fruits if they are not already present in the
|
// TODO: Insert new fruits if they are not already present in the
|
||||||
// basket. Note that you are not allowed to put any type of fruit that's
|
// basket. Note that you are not allowed to put any type of fruit that's
|
||||||
// already present!
|
// already present!
|
||||||
|
basket.entry(fruit).or_insert(1);
|
||||||
|
// if !basket.contains_key(&fruit) {
|
||||||
|
// basket.insert(fruit, 1);
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,7 +84,7 @@ mod tests {
|
|||||||
let count = basket.values().sum::<u32>();
|
let count = basket.values().sum::<u32>();
|
||||||
assert!(count > 11);
|
assert!(count > 11);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn all_fruit_types_in_basket() {
|
fn all_fruit_types_in_basket() {
|
||||||
let mut basket = get_fruit_basket();
|
let mut basket = get_fruit_basket();
|
||||||
|
|||||||
@ -15,8 +15,6 @@
|
|||||||
// Execute `rustlings hint hashmaps3` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint hashmaps3` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
// A structure to store the goal details of a team.
|
// A structure to store the goal details of a team.
|
||||||
@ -36,6 +34,15 @@ fn build_scores_table(results: String) -> HashMap<String, Team> {
|
|||||||
let team_2_name = v[1].to_string();
|
let team_2_name = v[1].to_string();
|
||||||
let team_2_score: u8 = v[3].parse().unwrap();
|
let team_2_score: u8 = v[3].parse().unwrap();
|
||||||
// TODO: Populate the scores table with details extracted from the
|
// TODO: Populate the scores table with details extracted from the
|
||||||
|
let s1 = scores.entry(team_1_name).or_insert(Team{ goals_scored: 0, goals_conceded: 0});
|
||||||
|
s1.goals_scored += team_1_score;
|
||||||
|
s1.goals_conceded += team_2_score;
|
||||||
|
|
||||||
|
let s2 = scores.entry(team_2_name).or_insert(Team{ goals_scored: 0, goals_conceded: 0});
|
||||||
|
s2.goals_conceded += team_1_score;
|
||||||
|
s2.goals_scored += team_2_score;
|
||||||
|
|
||||||
|
|
||||||
// current line. Keep in mind that goals scored by team_1
|
// current line. Keep in mind that goals scored by team_1
|
||||||
// will be the number of goals conceded by team_2, and similarly
|
// will be the number of goals conceded by team_2, and similarly
|
||||||
// goals scored by team_2 will be the number of goals conceded by
|
// goals scored by team_2 will be the number of goals conceded by
|
||||||
|
|||||||
@ -20,7 +20,6 @@
|
|||||||
//
|
//
|
||||||
// No hints this time!
|
// No hints this time!
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
pub enum Command {
|
pub enum Command {
|
||||||
Uppercase,
|
Uppercase,
|
||||||
@ -31,12 +30,14 @@ pub enum Command {
|
|||||||
mod my_module {
|
mod my_module {
|
||||||
use super::Command;
|
use super::Command;
|
||||||
|
|
||||||
// TODO: Complete the function signature!
|
pub fn transformer(input: Vec<(String, Command)>) -> Vec<String> {
|
||||||
pub fn transformer(input: ???) -> ??? {
|
let mut output: Vec<String> = vec![];
|
||||||
// TODO: Complete the output declaration!
|
|
||||||
let mut output: ??? = vec![];
|
|
||||||
for (string, command) in input.iter() {
|
for (string, command) in input.iter() {
|
||||||
// TODO: Complete the function body. You can do it!
|
output.push(match command {
|
||||||
|
Command::Uppercase => string.to_uppercase(),
|
||||||
|
Command::Trim => string.trim().to_string(),
|
||||||
|
Command::Append(i) => string.to_owned() + &String::from("bar").repeat(*i),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
output
|
output
|
||||||
}
|
}
|
||||||
@ -45,7 +46,7 @@ mod my_module {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
// TODO: What do we need to import to have `transformer` in scope?
|
// TODO: What do we need to import to have `transformer` in scope?
|
||||||
use ???;
|
use crate::my_module::transformer;
|
||||||
use super::Command;
|
use super::Command;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user