completed enums, modules, strings and structs

This commit is contained in:
Ruud Salym J Erie 2022-11-27 16:25:08 -03:00
parent 0d1db97a93
commit c50941b220
13 changed files with 69 additions and 59 deletions

View File

@ -1,11 +1,13 @@
// enums1.rs
// No hints this time! ;)
// I AM NOT DONE
#[derive(Debug)]
enum Message {
// TODO: define a few types of messages as used below
Quit,
Echo,
Move,
ChangeColor
}
fn main() {

View File

@ -1,11 +1,12 @@
// enums2.rs
// Execute `rustlings hint enums2` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
#[derive(Debug)]
enum Message {
// TODO: define the different variants used below
Quit,
Echo(String),
Move{x:i32,y:i32},
ChangeColor(i32,i32,i32)
}
impl Message {

View File

@ -2,10 +2,11 @@
// Address all the TODOs to make the tests pass!
// 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
Quit,
Echo(String),
Move(Point),
ChangeColor((u8,u8,u8))
}
struct Point {
@ -38,6 +39,12 @@ impl State {
fn process(&mut self, message: Message) {
// TODO: create a match expression to process the different message variants
match message {
Message::Quit => self.quit(),
Message::Echo(s) => self.echo(s),
Message::Move(m) => self.move_position(m),
Message::ChangeColor((r,g,b)) => self.change_color((r,g,b))
}
}
}

View File

@ -1,7 +1,6 @@
// modules1.rs
// 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!
@ -9,7 +8,7 @@ mod sausage_factory {
String::from("Ginger")
}
fn make_sausage() {
pub fn make_sausage() {
get_secret_recipe();
println!("sausage!");
}

View File

@ -3,12 +3,10 @@
// 'use' and 'as' keywords. Fix these 'use' statements to make the code compile.
// 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";

View File

@ -5,10 +5,9 @@
// from the std::time module. Bonus style points if you can do it with one line!
// 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) {

View File

@ -2,13 +2,11 @@
// Make me compile without changing the function signature!
// 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 {
"blue"
"blue".to_string()
}

View File

@ -2,11 +2,9 @@
// Make me compile without changing the function signature!
// 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 :)
if is_a_color_word(word) {
if is_a_color_word(word.as_str()) {
println!("That is a color word I know!");
} else {
println!("That is not a color word I know.");

View File

@ -1,21 +1,17 @@
// strings3.rs
// 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!
???
return String::from(input.trim());
}
fn compose_me(input: &str) -> String {
// TODO: Add " world!" to the string! There's multiple ways to do this!
???
return String::from(input.to_owned() + &String::from(" world!"));
}
fn replace_me(input: &str) -> String {
// TODO: Replace "cars" in the string with "balloons"!
???
String::from(input.replace("cars","balloons"))
}
#[cfg(test)]

View File

@ -6,8 +6,6 @@
// before the parentheses on each line. If you're right, it will compile!
// No hints this time!
// I AM NOT DONE
fn string_slice(arg: &str) {
println!("{}", arg);
}
@ -16,14 +14,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_slice("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());
}

View File

@ -2,16 +2,19 @@
// Address all the TODOs to make the tests pass!
// Execute `rustlings hint structs1` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
struct ColorClassicStruct {
// TODO: Something goes here
red: i32,
green: i32,
blue: i32,
}
struct ColorTupleStruct(/* TODO: Something goes here */);
struct ColorTupleStruct(i32,i32,i32);
#[derive(Debug)]
struct UnitLikeStruct;
struct UnitLikeStruct{}
#[cfg(test)]
mod tests {
@ -19,8 +22,12 @@ mod tests {
#[test]
fn classic_c_structs() {
// TODO: Instantiate a classic c struct!
// let green =
// DONE: Instantiate a classic c struct!
let green = ColorClassicStruct{
red: 0,
green: 255,
blue: 0
};
assert_eq!(green.red, 0);
assert_eq!(green.green, 255);
@ -29,8 +36,8 @@ mod tests {
#[test]
fn tuple_structs() {
// TODO: Instantiate a tuple struct!
// let green =
// DONE: Instantiate a tuple struct!
let green = ColorTupleStruct(0,255,0);
assert_eq!(green.0, 0);
assert_eq!(green.1, 255);
@ -39,8 +46,8 @@ mod tests {
#[test]
fn unit_structs() {
// TODO: Instantiate a unit-like struct!
// let unit_like_struct =
// DONE: Instantiate a unit-like struct!
let unit_like_struct = UnitLikeStruct{};
let message = format!("{:?}s are fun!", unit_like_struct);
assert_eq!(message, "UnitLikeStructs are fun!");

View File

@ -2,9 +2,8 @@
// Address all the TODOs to make the tests pass!
// Execute `rustlings hint structs2` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
#[derive(Debug)]
#[derive(Clone,Debug)]
struct Order {
name: String,
year: u32,
@ -35,7 +34,11 @@ mod tests {
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 = Order {
name: String::from("Hacker in Rust"),
count: 1,
..order_template
};
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);

View File

@ -4,7 +4,6 @@
// Make the code compile and the tests pass!
// Execute `rustlings hint structs3` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
#[derive(Debug)]
struct Package {
@ -26,12 +25,17 @@ impl Package {
}
}
fn is_international(&self) -> ??? {
// Something goes here...
fn is_international(&self) -> bool {
let mut is_international = false;
if &self.sender_country != &self.recipient_country{
is_international = true;
}
is_international
}
fn get_fees(&self, cents_per_gram: i32) -> ??? {
// Something goes here...
fn get_fees(&self, cents_per_gram: i32) -> i32 {
cents_per_gram * &self.weight_in_grams
}
}