mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-03 09:19:18 +00:00
more exercise
This commit is contained in:
parent
d9776cf9a7
commit
c29bf477b7
@ -5,10 +5,12 @@
|
||||
// Execute `rustlings hint enums3` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
enum Message {
|
||||
// TODO: implement the message variant types based on their usage below
|
||||
ChangeColor(u8,u8,u8),
|
||||
Echo(String),
|
||||
Move(Point),
|
||||
Quit
|
||||
}
|
||||
|
||||
struct Point {
|
||||
@ -44,6 +46,12 @@ impl State {
|
||||
// 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:
|
||||
// fn function((t, u, p, l, e))
|
||||
match message {
|
||||
Message::ChangeColor(r,g,b) => self.color = (r,g,b),
|
||||
Message::Echo(test) => self.message = test,
|
||||
Message::Move(test) => self.position = test,
|
||||
Message::Quit => self.quit = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -5,13 +5,12 @@
|
||||
// Execute `rustlings hint strings1` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
fn main() {
|
||||
let answer = current_favorite_color();
|
||||
println!("My current favorite color is {}", answer);
|
||||
}
|
||||
|
||||
fn current_favorite_color() -> String {
|
||||
fn current_favorite_color() -> &'static str {
|
||||
"blue"
|
||||
}
|
||||
|
||||
@ -5,7 +5,6 @@
|
||||
// Execute `rustlings hint strings2` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
fn main() {
|
||||
let word = String::from("green"); // Try not changing this line :)
|
||||
@ -16,6 +15,6 @@ fn main() {
|
||||
}
|
||||
}
|
||||
|
||||
fn is_a_color_word(attempt: &str) -> bool {
|
||||
fn is_a_color_word(attempt: String) -> bool {
|
||||
attempt == "green" || attempt == "blue" || attempt == "red"
|
||||
}
|
||||
|
||||
@ -3,21 +3,21 @@
|
||||
// Execute `rustlings hint strings3` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
fn trim_me(input: &str) -> String {
|
||||
// TODO: Remove whitespace from both ends of a string!
|
||||
???
|
||||
String::from(input.trim())
|
||||
}
|
||||
|
||||
fn compose_me(input: &str) -> String {
|
||||
// TODO: Add " world!" to the string! There's multiple ways to do this!
|
||||
???
|
||||
let rv = String::from(input) + " world!";
|
||||
rv
|
||||
}
|
||||
|
||||
fn replace_me(input: &str) -> String {
|
||||
// TODO: Replace "cars" in the string with "balloons"!
|
||||
???
|
||||
input.replace("cars","balloons")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@ -7,7 +7,6 @@
|
||||
//
|
||||
// No hints this time!
|
||||
|
||||
// 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("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());
|
||||
}
|
||||
|
||||
@ -3,7 +3,6 @@
|
||||
// Execute `rustlings hint modules1` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
mod sausage_factory {
|
||||
// Don't let anybody outside of this module see this!
|
||||
@ -11,7 +10,7 @@ mod sausage_factory {
|
||||
String::from("Ginger")
|
||||
}
|
||||
|
||||
fn make_sausage() {
|
||||
pub fn make_sausage() {
|
||||
get_secret_recipe();
|
||||
println!("sausage!");
|
||||
}
|
||||
|
||||
@ -7,12 +7,11 @@
|
||||
// Execute `rustlings hint modules2` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
mod delicious_snacks {
|
||||
// TODO: Fix these use statements
|
||||
use self::fruits::PEAR as ???
|
||||
use self::veggies::CUCUMBER as ???
|
||||
pub use self::fruits::PEAR as fruit;
|
||||
pub use self::veggies::CUCUMBER as veggie;
|
||||
|
||||
mod fruits {
|
||||
pub const PEAR: &'static str = "Pear";
|
||||
|
||||
@ -8,10 +8,9 @@
|
||||
// Execute `rustlings hint modules3` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
// TODO: Complete this use statement
|
||||
use ???
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
fn main() {
|
||||
match SystemTime::now().duration_since(UNIX_EPOCH) {
|
||||
|
||||
@ -11,18 +11,18 @@
|
||||
// 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"), 2);
|
||||
basket.insert(String::from("gum-gum"), 2);
|
||||
basket
|
||||
}
|
||||
|
||||
|
||||
@ -14,7 +14,6 @@
|
||||
// Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
@ -40,6 +39,9 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
|
||||
// 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
|
||||
// already present!
|
||||
if !basket.contains_key(&fruit) {
|
||||
basket.insert(fruit, 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -14,7 +14,6 @@
|
||||
// Execute `rustlings hint hashmaps3` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
@ -39,6 +38,12 @@ fn build_scores_table(results: String) -> HashMap<String, Team> {
|
||||
// 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
|
||||
// team_1.
|
||||
scores.entry(team_1_name)
|
||||
.and_modify(|team| {team.goals_scored = team.goals_scored + team_1_score; team.goals_conceded= team.goals_conceded + team_2_score;})
|
||||
.or_insert(Team {goals_scored: team_1_score, goals_conceded: team_2_score});
|
||||
scores.entry(team_2_name)
|
||||
.and_modify(|team| {team.goals_scored= team.goals_scored + team_2_score; team.goals_conceded= team.goals_conceded + team_1_score;})
|
||||
.or_insert(Team {goals_scored: team_2_score, goals_conceded: team_1_score});
|
||||
}
|
||||
scores
|
||||
}
|
||||
|
||||
@ -3,7 +3,6 @@
|
||||
// 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
|
||||
@ -13,7 +12,12 @@ fn maybe_icecream(time_of_day: u16) -> Option<u16> {
|
||||
// value of 0 The Option output should gracefully handle cases where
|
||||
// time_of_day > 23.
|
||||
// TODO: Complete the function body - remember to return an Option!
|
||||
???
|
||||
if time_of_day < 22 {
|
||||
return Some(5)
|
||||
} else if time_of_day > 21 && time_of_day < 24 {
|
||||
return Some(0)
|
||||
}
|
||||
return None
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@ -34,6 +38,6 @@ mod tests {
|
||||
// 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(5));
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,7 +3,6 @@
|
||||
// Execute `rustlings hint options2` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
@ -13,7 +12,7 @@ mod tests {
|
||||
let optional_target = Some(target);
|
||||
|
||||
// TODO: Make this an if let statement whose value is "Some" type
|
||||
word = optional_target {
|
||||
if let Some(word) = optional_target {
|
||||
assert_eq!(word, target);
|
||||
}
|
||||
}
|
||||
@ -32,7 +31,7 @@ mod tests {
|
||||
// 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() {
|
||||
while let Some(Some(integer)) = optional_integers.pop() {
|
||||
assert_eq!(integer, cursor);
|
||||
cursor -= 1;
|
||||
}
|
||||
|
||||
@ -3,7 +3,6 @@
|
||||
// Execute `rustlings hint options3` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
struct Point {
|
||||
x: i32,
|
||||
@ -13,7 +12,7 @@ struct Point {
|
||||
fn main() {
|
||||
let y: Option<Point> = Some(Point { x: 100, y: 200 });
|
||||
|
||||
match y {
|
||||
match &y {
|
||||
Some(p) => println!("Co-ordinates are {},{} ", p.x, p.y),
|
||||
_ => panic!("no match!"),
|
||||
}
|
||||
|
||||
@ -20,7 +20,6 @@
|
||||
//
|
||||
// No hints this time!
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
pub enum Command {
|
||||
Uppercase,
|
||||
@ -32,10 +31,15 @@ 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() {
|
||||
output.push(match command {
|
||||
Command::Uppercase => string.to_uppercase().to_string(),
|
||||
Command::Trim => string.trim().to_string(),
|
||||
Command::Append(s) => string.to_string() + &"bar".repeat(*s)
|
||||
});
|
||||
// TODO: Complete the function body. You can do it!
|
||||
}
|
||||
output
|
||||
@ -45,7 +49,7 @@ mod my_module {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
// TODO: What do we need to import to have `transformer` in scope?
|
||||
use ???;
|
||||
use super::my_module::transformer;
|
||||
use super::Command;
|
||||
|
||||
#[test]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user