Skip to content

Commit

Permalink
The enums and Strings code sections are complete
Browse files Browse the repository at this point in the history
  • Loading branch information
Looop1g committed Jan 5, 2025
1 parent c6cfeb6 commit 1a8d12f
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 32 deletions.
6 changes: 4 additions & 2 deletions exercises/enums/enums1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
//
// 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() {
Expand Down
6 changes: 4 additions & 2 deletions exercises/enums/enums2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
// 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
Move { x: i32, y: i32 },
Echo(String),
ChangeColor(i32, i32, i32),
Quit,
}

impl Message {
Expand Down
18 changes: 14 additions & 4 deletions exercises/enums/enums3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -20,7 +22,7 @@ struct State {
color: (u8, u8, u8),
position: Point,
quit: bool,
message: String
message: String,
}

impl State {
Expand All @@ -32,7 +34,9 @@ impl State {
self.quit = true;
}

fn echo(&mut self, s: String) { self.message = s }
fn echo(&mut self, s: String) {
self.message = s
}

fn move_position(&mut self, p: Point) {
self.position = p;
Expand All @@ -43,6 +47,12 @@ impl State {
// 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.change_color((r, g, b)),
Message::Echo(str) => self.echo(str),
Message::Move(p) => self.move_position(p),
Message::Quit => self.quit(),
}
}
}

Expand Down
4 changes: 1 addition & 3 deletions exercises/strings/strings1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
// 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()
}
4 changes: 1 addition & 3 deletions exercises/strings/strings2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +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 :)
if is_a_color_word(word) {
Expand All @@ -16,6 +14,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"
}
18 changes: 12 additions & 6 deletions exercises/strings/strings3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
// Execute `rustlings hint strings3` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE
use std::os::unix::raw::ino_t;

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 {
// TODO: Add " world!" to the string! There's multiple ways to do this!
???
input.to_string() + " world!"
}

fn replace_me(input: &str) -> String {
// TODO: Replace "cars" in the string with "balloons"!
???
input.to_string().replace("cars", "balloons")
}

#[cfg(test)]
Expand All @@ -39,7 +39,13 @@ mod tests {

#[test]
fn replace_a_string() {
assert_eq!(replace_me("I think cars are cool"), "I think balloons are cool");
assert_eq!(replace_me("I love to look at cars"), "I love to look at balloons");
assert_eq!(
replace_me("I think cars are cool"),
"I think balloons are cool"
);
assert_eq!(
replace_me("I love to look at cars"),
"I love to look at balloons"
);
}
}
22 changes: 10 additions & 12 deletions exercises/strings/strings4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
//
// No hints this time!

// I AM NOT DONE

fn string_slice(arg: &str) {
println!("{}", arg);
}
Expand All @@ -17,14 +15,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());
}

0 comments on commit 1a8d12f

Please sign in to comment.