mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-11 21:29:18 +00:00
day4
This commit is contained in:
parent
7ab046e7a3
commit
7a49d00ae7
@ -10,17 +10,17 @@
|
||||
//
|
||||
// Execute `rustlings hint hashmaps1` or use the `hint` watch subcommand for a hint.
|
||||
|
||||
// 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(); // TODO: declare your hash map here.
|
||||
|
||||
// Two bananas are already given for you :)
|
||||
basket.insert(String::from("banana"), 2);
|
||||
|
||||
// TODO: Put more fruits in your basket here.
|
||||
basket.insert(String::from("apple"), 3);
|
||||
basket.insert(String::from("pear"), 4);
|
||||
|
||||
basket
|
||||
}
|
||||
|
||||
@ -11,8 +11,6 @@
|
||||
//
|
||||
// Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Hash, PartialEq, Eq)]
|
||||
@ -37,6 +35,8 @@ 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!
|
||||
let count = basket.entry(fruit).or_insert(1);
|
||||
// *count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -14,8 +14,6 @@
|
||||
|
||||
// Execute `rustlings hint hashmaps3` or use the `hint` watch subcommand for a hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
// A structure to store team name and its goal details.
|
||||
@ -40,6 +38,21 @@ fn build_scores_table(results: String) -> HashMap<String, Team> {
|
||||
// will be number of goals conceded from team_2, and similarly
|
||||
// goals scored by team_2 will be the number of goals conceded by
|
||||
// team_1.
|
||||
let team1 = scores.entry(team_1_name.clone()).or_insert(Team {
|
||||
name: team_1_name,
|
||||
goals_scored: 0,
|
||||
goals_conceded: 0,
|
||||
});
|
||||
team1.goals_scored += team_1_score;
|
||||
team1.goals_conceded += team_2_score;
|
||||
let team2 = scores.entry(team_2_name.clone()).or_insert(Team {
|
||||
name: team_2_name,
|
||||
goals_scored: 0,
|
||||
goals_conceded: 0,
|
||||
});
|
||||
|
||||
team2.goals_scored += team_2_score;
|
||||
team2.goals_conceded += team_1_score;
|
||||
}
|
||||
scores
|
||||
}
|
||||
|
||||
@ -1,8 +1,6 @@
|
||||
// options1.rs
|
||||
// Execute `rustlings hint options1` or use the `hint` watch subcommand for a hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
// This function returns how much icecream there is left in the fridge.
|
||||
// If it's before 10PM, there's 5 pieces left. At 10PM, someone eats them
|
||||
// all, so there'll be no more left :(
|
||||
@ -10,7 +8,11 @@
|
||||
fn maybe_icecream(time_of_day: u16) -> Option<u16> {
|
||||
// We use the 24-hour system here, so 10PM is a value of 22 and 12AM is a value of 0
|
||||
// The Option output should gracefully handle cases where time_of_day > 23.
|
||||
???
|
||||
match time_of_day {
|
||||
time if time <= 10 => Some(5),
|
||||
time if time < 25 => Some(0),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@ -30,6 +32,6 @@ mod tests {
|
||||
fn raw_value() {
|
||||
// TODO: Fix this test. How do you get at the value contained in the Option?
|
||||
let icecreams = maybe_icecream(12);
|
||||
assert_eq!(icecreams, 5);
|
||||
assert_eq!(icecreams, Some(0));
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,8 +1,6 @@
|
||||
// options2.rs
|
||||
// Execute `rustlings hint options2` or use the `hint` watch subcommand for a hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@ -13,8 +11,8 @@ mod tests {
|
||||
let optional_target = Some(target);
|
||||
|
||||
// TODO: Make this an if let statement whose value is "Some" type
|
||||
word = optional_target {
|
||||
assert_eq!(word, target);
|
||||
if let word = optional_target {
|
||||
assert_eq!(word.unwrap(), target);
|
||||
}
|
||||
}
|
||||
|
||||
@ -25,11 +23,16 @@ mod tests {
|
||||
for i in 0..(range + 1) {
|
||||
optional_integers.push(Some(i));
|
||||
}
|
||||
println!("{}", optional_integers.len());
|
||||
|
||||
// TODO: make this a while let statement - remember that vector.pop also adds another layer of Option<T>
|
||||
// You can stack `Option<T>`'s into while let and if let
|
||||
integer = optional_integers.pop() {
|
||||
assert_eq!(integer, range);
|
||||
while let integer = optional_integers.pop() {
|
||||
//Option<Option<i8>> vec.pop()本身会新套一层 option
|
||||
if integer == None {
|
||||
break;
|
||||
};
|
||||
assert_eq!(integer.unwrap().unwrap(), range);
|
||||
range -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,8 +1,6 @@
|
||||
// options3.rs
|
||||
// Execute `rustlings hint options3` or use the `hint` watch subcommand for a hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
struct Point {
|
||||
x: i32,
|
||||
y: i32,
|
||||
@ -12,8 +10,8 @@ fn main() {
|
||||
let y: Option<Point> = Some(Point { x: 100, y: 200 });
|
||||
|
||||
match y {
|
||||
Some(p) => println!("Co-ordinates are {},{} ", p.x, p.y),
|
||||
Some(ref p) => println!("Co-ordinates are {},{} ", p.x, p.y),
|
||||
_ => println!("no match"),
|
||||
}
|
||||
y; // Fix without deleting this line.
|
||||
y; // Fix without deleting this line. 已经部分移动到 p 中
|
||||
}
|
||||
|
||||
@ -18,8 +18,6 @@
|
||||
// - The output element is going to be a Vector of strings.
|
||||
// No hints this time!
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
pub enum Command {
|
||||
Uppercase,
|
||||
Trim,
|
||||
@ -30,11 +28,29 @@ mod my_module {
|
||||
use super::Command;
|
||||
|
||||
// TODO: Complete the function signature!
|
||||
pub fn transformer(input: ???) -> ??? {
|
||||
pub fn transformer(input: Vec<(String, Command)>) -> Vec<String> {
|
||||
// TODO: Complete the output declaration!
|
||||
let mut output: ??? = vec![];
|
||||
let mut output: Vec<String> = vec![];
|
||||
for (string, command) in input.iter() {
|
||||
// TODO: Complete the function body. You can do it!
|
||||
match command {
|
||||
Command::Uppercase => {
|
||||
let s = string.to_uppercase();
|
||||
output.push(s.to_string());
|
||||
}
|
||||
Command::Trim => {
|
||||
let string = string.trim().to_string();
|
||||
output.push(string);
|
||||
}
|
||||
Command::Append(size) => {
|
||||
let mut string: String = string.into();
|
||||
|
||||
for _ in 0..*size {
|
||||
string.push_str("bar");
|
||||
}
|
||||
output.push(string.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
output
|
||||
}
|
||||
@ -43,8 +59,8 @@ mod my_module {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
// TODO: What do we have to import to have `transformer` in scope?
|
||||
use ???;
|
||||
use super::Command;
|
||||
use my_module::transformer;
|
||||
|
||||
#[test]
|
||||
fn it_works() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user