Ejercicios 40/94

This commit is contained in:
perro tuerto 2023-02-20 18:26:18 -08:00
parent 99745f1acf
commit 0cfdad15d9
8 changed files with 37 additions and 42 deletions

View File

@ -1,11 +1,12 @@
// enums1.rs // enums1.rs
// 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() {

View File

@ -1,11 +1,12 @@
// enums2.rs // enums2.rs
// Execute `rustlings hint enums2` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint enums2` or use the `hint` watch subcommand for a 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(u32, u32, u32),
Quit
} }
impl Message { impl Message {

View File

@ -2,10 +2,11 @@
// Address all the TODOs to make the tests pass! // Address all the TODOs to make the tests pass!
// Execute `rustlings hint enums3` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint enums3` or use the `hint` watch subcommand for a 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 {
@ -37,8 +38,13 @@ 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
// Remember: When passing a tuple as a function argument, you'll need extra parentheses: fn function((t, u, p, l, e)) // Remember: When passing a tuple as a function argument, you'll need extra parentheses: fn function((t, u, p, l, e))
match message {
Message::Quit => self.quit(),
Message::Echo(string) => self.echo(string),
Message::Move(point) => self.move_position(point),
Message::ChangeColor(r, g, b) => self.change_color((r, g, b)),
}
} }
} }

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` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint strings1` or use the `hint` watch subcommand for a hint.
// 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" String::from("blue")
} }

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` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint strings2` or use the `hint` watch subcommand for a 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.");

View File

@ -1,21 +1,16 @@
// strings3.rs // strings3.rs
// Execute `rustlings hint strings3` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint strings3` or use the `hint` watch subcommand for a 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! input.trim().to_string()
???
} }
fn compose_me(input: &str) -> String { fn compose_me(input: &str) -> String {
// TODO: Add " world!" to the string! There's multiple ways to do this! format!("{}{}", input, " world!")
???
} }
fn replace_me(input: &str) -> String { fn replace_me(input: &str) -> String {
// TODO: Replace "cars" in the string with "balloons"! input.replace("cars", "balloons")
???
} }
#[cfg(test)] #[cfg(test)]

View File

@ -6,8 +6,6 @@
// 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!
// 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);
} }
@ -16,14 +14,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

@ -4,8 +4,6 @@
// Make the code compile and the tests pass! // Make the code compile and the tests pass!
// Execute `rustlings hint structs3` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint structs3` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
#[derive(Debug)] #[derive(Debug)]
struct Package { struct Package {
sender_country: String, sender_country: String,
@ -26,12 +24,12 @@ impl Package {
} }
} }
fn is_international(&self) -> ??? { fn is_international(&self) -> bool {
// Something goes here... if self.sender_country == self.recipient_country { false } else { true }
} }
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
} }
} }