mirror of
https://git.aramjonghu.dev/AramJonghu/rustlings.git
synced 2026-09-07 10:13:31 +02:00
Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ad542a4892 | ||
|
|
8b15f8d511 | ||
|
|
2844976587 | ||
|
|
f0f6a72101 | ||
|
|
3caf0b31ec |
@@ -1,49 +0,0 @@
|
||||
#[derive(Debug)]
|
||||
struct Order {
|
||||
name: String,
|
||||
year: u32,
|
||||
made_by_phone: bool,
|
||||
made_by_mobile: bool,
|
||||
made_by_email: bool,
|
||||
item_number: u32,
|
||||
count: u32,
|
||||
}
|
||||
|
||||
fn create_order_template() -> Order {
|
||||
Order {
|
||||
name: String::from("Bob"),
|
||||
year: 2019,
|
||||
made_by_phone: false,
|
||||
made_by_mobile: false,
|
||||
made_by_email: true,
|
||||
item_number: 123,
|
||||
count: 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// You can optionally experiment here.
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn your_order() {
|
||||
let order_template = create_order_template();
|
||||
|
||||
// TODO: Create your own order using the update syntax and template above!
|
||||
// let your_order =
|
||||
|
||||
let your_order = ;
|
||||
|
||||
assert_eq!(your_order.name, "Hacker in Rust");
|
||||
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_mobile, order_template.made_by_mobile);
|
||||
assert_eq!(your_order.made_by_email, order_template.made_by_email);
|
||||
assert_eq!(your_order.item_number, order_template.item_number);
|
||||
assert_eq!(your_order.count, 1);
|
||||
}
|
||||
}
|
||||
@@ -10,19 +10,18 @@ struct Fireworks {
|
||||
rockets: usize,
|
||||
}
|
||||
|
||||
// TODO: Turn this function into an associated function on `Fireworks`.
|
||||
fn new_fireworks() -> Fireworks {
|
||||
Fireworks { rockets: 0 }
|
||||
}
|
||||
impl Fireworks {
|
||||
fn new() -> Self {
|
||||
Self { rockets: 0 }
|
||||
}
|
||||
|
||||
// TODO: Turn this function into a method on `Fireworks`.
|
||||
fn add_rockets(fireworks: &mut Fireworks, rockets: usize) {
|
||||
fireworks.rockets += rockets
|
||||
}
|
||||
fn add_rockets(&mut self, rockets: usize) {
|
||||
self.rockets += rockets
|
||||
}
|
||||
|
||||
// TODO: Turn this function into a method on `Fireworks`.
|
||||
fn start(fireworks: Fireworks) -> String {
|
||||
"🚀".repeat(fireworks.rockets)
|
||||
fn start(self) -> String {
|
||||
"🚀".repeat(self.rockets)
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
#[derive(Debug)]
|
||||
enum Message {
|
||||
// TODO: Define a few types of messages as used below.
|
||||
Resize,
|
||||
Move,
|
||||
Echo,
|
||||
ChangeColor,
|
||||
Quit,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
@@ -7,6 +7,11 @@ struct Point {
|
||||
#[derive(Debug)]
|
||||
enum Message {
|
||||
// TODO: Define the different variants used below.
|
||||
Resize { width: u64, height: u64 },
|
||||
Move(Point),
|
||||
Echo(String),
|
||||
ChangeColor(u64, u64, u64),
|
||||
Quit,
|
||||
}
|
||||
|
||||
impl Message {
|
||||
|
||||
@@ -46,6 +46,13 @@ impl State {
|
||||
fn process(&mut self, message: Message) {
|
||||
// TODO: Create a match expression to process the different message
|
||||
// variants using the methods defined above.
|
||||
match message {
|
||||
Message::Resize { width, height } => self.resize(width, height),
|
||||
Message::Move(point) => self.move_position(point),
|
||||
Message::Echo(s) => self.echo(s),
|
||||
Message::ChangeColor(red, green, blue) => self.change_color(red, green, blue),
|
||||
Message::Quit => self.quit(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// TODO: Fix the compiler error without changing the function signature.
|
||||
fn current_favorite_color() -> String {
|
||||
"blue"
|
||||
"blue".to_string()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
@@ -6,7 +6,7 @@ fn is_a_color_word(attempt: &str) -> bool {
|
||||
fn main() {
|
||||
let word = String::from("green"); // Don't change this line.
|
||||
|
||||
if is_a_color_word(word) {
|
||||
if is_a_color_word(&word) {
|
||||
println!("That is a color word I know!");
|
||||
} else {
|
||||
println!("That is not a color word I know.");
|
||||
|
||||
@@ -1,13 +1,18 @@
|
||||
fn trim_me(input: &str) -> &str {
|
||||
// TODO: Remove whitespace from both ends of a string.
|
||||
input.trim()
|
||||
}
|
||||
|
||||
fn compose_me(input: &str) -> String {
|
||||
// TODO: Add " world!" to the string! There are multiple ways to do this.
|
||||
let world = " world!";
|
||||
let s: String = input.to_owned() + world;
|
||||
s
|
||||
}
|
||||
|
||||
fn replace_me(input: &str) -> String {
|
||||
// TODO: Replace "cars" in the string with "balloons".
|
||||
input.replace("cars", "balloons")
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
@@ -13,23 +13,23 @@ fn string(arg: String) {
|
||||
// Your task is to replace `placeholder(…)` with either `string_slice(…)`
|
||||
// or `string(…)` depending on what you think each value is.
|
||||
fn main() {
|
||||
placeholder("blue");
|
||||
string_slice("blue");
|
||||
|
||||
placeholder("red".to_string());
|
||||
string("red".to_string());
|
||||
|
||||
placeholder(String::from("hi"));
|
||||
string(String::from("hi"));
|
||||
|
||||
placeholder("rust is fun!".to_owned());
|
||||
string("rust is fun!".to_owned());
|
||||
|
||||
placeholder(format!("Interpolation {}", "Station"));
|
||||
string(format!("Interpolation {}", "Station"));
|
||||
|
||||
// WARNING: This is byte indexing, not character indexing.
|
||||
// Character indexing can be done using `s.chars().nth(INDEX)`.
|
||||
placeholder(&String::from("abc")[0..1]);
|
||||
string_slice(&String::from("abc")[0..1]);
|
||||
|
||||
placeholder(" hello there ".trim());
|
||||
string_slice(" hello there ".trim());
|
||||
|
||||
placeholder("Happy Monday!".replace("Mon", "Tues"));
|
||||
string("Happy Monday!".replace("Mon", "Tues"));
|
||||
|
||||
placeholder("mY sHiFt KeY iS sTiCkY".to_lowercase());
|
||||
string("mY sHiFt KeY iS sTiCkY".to_lowercase());
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ mod sausage_factory {
|
||||
String::from("Ginger")
|
||||
}
|
||||
|
||||
fn make_sausage() {
|
||||
pub fn make_sausage() {
|
||||
get_secret_recipe();
|
||||
println!("sausage!");
|
||||
}
|
||||
|
||||
@@ -5,6 +5,9 @@ mod delicious_snacks {
|
||||
// TODO: Add the following two `use` statements after fixing them.
|
||||
// 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: &str = "Pear";
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
// TODO: Bring `SystemTime` and `UNIX_EPOCH` from the `std::time` module into
|
||||
// your scope. Bonus style points if you can do it with one line!
|
||||
// use ???;
|
||||
use std::time::SystemTime;
|
||||
use std::time::UNIX_EPOCH;
|
||||
|
||||
fn main() {
|
||||
match SystemTime::now().duration_since(UNIX_EPOCH) {
|
||||
|
||||
@@ -9,10 +9,11 @@ use std::collections::HashMap;
|
||||
fn fruit_basket() -> HashMap<String, u32> {
|
||||
// TODO: Declare the hash map.
|
||||
// let mut basket =
|
||||
|
||||
let mut basket = HashMap::new();
|
||||
// Two bananas are already given for you :)
|
||||
basket.insert(String::from("banana"), 2);
|
||||
|
||||
basket.insert(String::from("apple"), 2);
|
||||
basket.insert(String::from("mango"), 1);
|
||||
// TODO: Put more fruits in your basket.
|
||||
|
||||
basket
|
||||
|
||||
@@ -28,10 +28,12 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
|
||||
Fruit::Pineapple,
|
||||
];
|
||||
|
||||
for fruit in fruit_kinds {
|
||||
for _fruit in fruit_kinds {
|
||||
// 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!
|
||||
basket.insert(Fruit::Banana, 4);
|
||||
basket.insert(Fruit::Pineapple, 3);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -27,6 +27,11 @@ fn build_scores_table(results: &str) -> HashMap<&str, TeamScores> {
|
||||
let team_1_score: u8 = split_iterator.next().unwrap().parse().unwrap();
|
||||
let team_2_score: u8 = split_iterator.next().unwrap().parse().unwrap();
|
||||
|
||||
scores.entry(team_1_name).or_default().goals_scored += team_1_score;
|
||||
scores.entry(team_2_name).or_default().goals_scored += team_2_score;
|
||||
|
||||
scores.entry(team_1_name).or_default().goals_conceded += team_2_score;
|
||||
scores.entry(team_2_name).or_default().goals_conceded += team_1_score;
|
||||
// TODO: Populate the scores table with the extracted details.
|
||||
// Keep in mind that goals scored by team 1 will be the number of goals
|
||||
// conceded by team 2. Similarly, goals scored by team 2 will be the
|
||||
|
||||
@@ -4,6 +4,14 @@
|
||||
// `hour_of_day` is higher than 23.
|
||||
fn maybe_ice_cream(hour_of_day: u16) -> Option<u16> {
|
||||
// TODO: Complete the function body.
|
||||
|
||||
if hour_of_day < 22 {
|
||||
Some(5)
|
||||
} else if hour_of_day <= 23 {
|
||||
Some(0)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
@@ -18,7 +26,7 @@ mod tests {
|
||||
fn raw_value() {
|
||||
// TODO: Fix this test. How do you get the value contained in the
|
||||
// Option?
|
||||
let ice_creams = maybe_ice_cream(12);
|
||||
let ice_creams = maybe_ice_cream(12).unwrap();
|
||||
|
||||
assert_eq!(ice_creams, 5); // Don't change this line.
|
||||
}
|
||||
|
||||
@@ -10,8 +10,12 @@ mod tests {
|
||||
let optional_target = Some(target);
|
||||
|
||||
// TODO: Make this an if-let statement whose value is `Some`.
|
||||
word = optional_target {
|
||||
assert_eq!(word, target);
|
||||
//
|
||||
// word = optional_target {
|
||||
// assert_eq!(word, target);
|
||||
// }
|
||||
if let Some(word) = optional_target {
|
||||
assert_eq!(word, target)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +33,13 @@ mod tests {
|
||||
// TODO: Make this a while-let statement. Remember that `Vec::pop()`
|
||||
// adds another layer of `Option`. You can do nested pattern matching
|
||||
// in if-let and while-let statements.
|
||||
integer = optional_integers.pop() {
|
||||
//
|
||||
// integer = optional_integers.pop() {
|
||||
// assert_eq!(integer, cursor);
|
||||
// cursor -= 1;
|
||||
// }
|
||||
|
||||
while let Some(Some(integer)) = optional_integers.pop() {
|
||||
assert_eq!(integer, cursor);
|
||||
cursor -= 1;
|
||||
}
|
||||
|
||||
@@ -9,7 +9,9 @@ fn main() {
|
||||
|
||||
// TODO: Fix the compiler error by adding something to this match statement.
|
||||
match optional_point {
|
||||
Some(p) => println!("Coordinates are {},{}", p.x, p.y),
|
||||
Some(ref p) => {
|
||||
println!("Coordinates are {},{}", p.x, p.y)
|
||||
}
|
||||
_ => panic!("No match!"),
|
||||
}
|
||||
|
||||
|
||||
@@ -28,6 +28,25 @@ mod my_module {
|
||||
|
||||
// TODO: Complete the function as described above.
|
||||
// pub fn transformer(input: ???) -> ??? { ??? }
|
||||
|
||||
pub fn transformer(input: Vec<(String, Command)>) -> Vec<String> {
|
||||
let mut output = Vec::new();
|
||||
for (s, cmd) in input {
|
||||
let transformed = match cmd {
|
||||
Command::Uppercase => s.to_uppercase(),
|
||||
Command::Trim => s.trim().to_owned(),
|
||||
Command::Append(n) => {
|
||||
let mut out = s;
|
||||
for _ in 0..n {
|
||||
out.push_str("bar");
|
||||
}
|
||||
out
|
||||
}
|
||||
};
|
||||
output.push(transformed)
|
||||
}
|
||||
output
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
@@ -39,6 +58,7 @@ mod tests {
|
||||
// TODO: What do we need to import to have `transformer` in scope?
|
||||
// use ???;
|
||||
use super::Command;
|
||||
use crate::my_module::transformer;
|
||||
|
||||
#[test]
|
||||
fn it_works() {
|
||||
|
||||
Reference in New Issue
Block a user