Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions exercises/complex-type/src/enums.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
// Exercise 1
// Fill in the blank and fix the errors
// Make it compile
#[derive(Debug)]
enum MessageOne {
Quit,
Move { x: i32, y: i32 },
Write(String),
ChangeColor(i32, i32, i32),
}
fn show_message(msg: MessageOne) {
println!("{}", msg);
println!("{:?}", msg);
}

fn exercise1() {
let msgs: __ = [
let msgs = [
MessageOne::Quit,
MessageOne::Move { x: 1, y: 3 },
MessageOne::ChangeColor(255, 255, 0),
Expand All @@ -29,6 +30,10 @@ fn exercise1() {
// Run tests
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 Down Expand Up @@ -60,6 +65,12 @@ impl State {
}

fn process(&mut self, message: Message) {
match message {
Message::ChangeColor(a,b ,c ) => self.change_color((a, b, c)),
Message::Echo(s) => self.echo(s),
Message::Move(p) => self.move_position(p),
Message::Quit => self.quit(),
}
// 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))
}
Expand All @@ -69,6 +80,7 @@ impl State {
// Exercise 3
// Fix the errors
// Run tests
#[derive(Debug, PartialEq)]
enum Direction {
North,
East,
Expand All @@ -79,7 +91,10 @@ enum Direction {
impl Direction {
fn opposite(&self) -> Direction {
match self {
//TODO
Direction::North => Direction::South,
Direction::East => Direction::West,
Direction::West => Direction::East,
Direction::South => Direction::North,
}
}
}
Expand All @@ -99,7 +114,10 @@ enum Operation {
// Perform arithmetic operations
fn perform_operation(operation: Operation, num1: f64, num2: f64) -> f64 {
match operation {
// TODO
Operation::Add => num1 + num2,
Operation::Subtract => num1 - num2,
Operation::Divide => num1 / num2,
Operation::Multiply => num1 * num2,
}
}

Expand Down
49 changes: 31 additions & 18 deletions exercises/complex-type/src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@
// Fix the error
// Make it compile
// Run test
#[derive(Debug)]
struct Person {
name: String,
age: u8,
hobby: String
}
impl PartialEq for Person
{
fn eq(&self, other: &Self) -> bool {
if self.name == other.name && self.age == other.age && self.hobby == other.hobby {
return true;
}
false
}
}
fn exercise1() -> Person {
let age = 30;
// Hobby = Rust
Expand All @@ -33,18 +43,18 @@ struct Agent {
// Implementation of methods for the Person struct
impl Agent {
// Create a new Person instance
fn new(name: String, age: u32) -> Agent {
Agent { name, age }
fn new(name: String, age: u32) -> Self {
Self { name, age }
}

// Get the name of the person
fn get_name(&self) -> &str {
todo!()
fn get_name(&self) -> String {
self.name.to_string()
}

// Get the age of the person
fn get_age(&self) -> u32 {
todo!()
self.age
}
}

Expand All @@ -58,28 +68,28 @@ struct Calculator {

impl Calculator {
fn new() -> Self {
Calculator { value: 0 }
Self { value: 0 }
}

fn add(&self, num: i32) {
fn add(&mut self, num: i32) {
self.value += num;
}

fn subtract(mut self, num: i32) {
fn subtract(&mut self, num: i32) {
self.value -= num;
}
fn clear(self) {
fn clear(&mut self) {
self.value = 0;
}

fn get_value(self) -> i32 {
fn get_value(&self) -> i32 {
self.value
}
}

// Exercise 4
// Make it compile
#[derive(Debug)]
#[derive(Debug, Clone)]
struct User {
first: String,
last: String,
Expand All @@ -95,7 +105,7 @@ fn exercise4() {

let u2 = User {
first: String::from("Mary"),
..u1
..u1.clone()

};

Expand All @@ -122,10 +132,10 @@ fn exercise5() {
});


let moved = foos[0];
let moved = &foos[0];


let moved_field = foos[0].str_val;
let moved_field = &foos[0].str_val;
}

// Exercise 6
Expand Down Expand Up @@ -153,12 +163,12 @@ impl Package {
}
}

fn is_international(&self) -> ??? {
// Something goes here...
fn is_international(&self) -> bool {
!self.sender_country.eq_ignore_ascii_case(self.recipient_country.as_str())
}

fn get_fees(&self, cents_per_gram: i32) -> ??? {
// Something goes here...
fn get_fees(&self, cents_per_gram: i32) -> i32 {
self.weight_in_grams * cents_per_gram
}
}

Expand All @@ -177,6 +187,9 @@ mod tests {
hobby:String::from("Rust")
};
assert_eq!(p, p_expectation);
// assert_eq!(p.name, p_expectation.name);
// assert_eq!(p.age, p_expectation.age);
// assert_eq!(p.hobby, p_expectation.hobby);

}

Expand Down