Skip to content

Commit

Permalink
my first session, everything upto and including enums
Browse files Browse the repository at this point in the history
  • Loading branch information
maradude committed Mar 23, 2022
1 parent d57c183 commit 18672fa
Show file tree
Hide file tree
Showing 31 changed files with 67 additions and 73 deletions.
6 changes: 4 additions & 2 deletions exercises/enums/enums1.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// enums1.rs
// Make me compile! Execute `rustlings hint enums1` for hints!

// 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
@@ -1,11 +1,13 @@
// enums2.rs
// Make me compile! Execute `rustlings hint enums2` for hints!

// I AM NOT DONE

#[derive(Debug)]
enum Message {
// TODO: define the different variants used below
Move { x: usize, y: usize},
Echo (String),
ChangeColor (u8, u8, u8),
Quit,
}

impl Message {
Expand Down
13 changes: 10 additions & 3 deletions exercises/enums/enums3.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// enums3.rs
// Address all the TODOs to make the tests pass!

// 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 Down Expand Up @@ -36,7 +38,12 @@ impl State {
}

fn process(&mut self, message: Message) {
// TODO: create a match expression to process the different message variants
match message {
Message::ChangeColor(i) => self.change_color(i),
Message::Echo(i) => self.echo(i),
Message::Move(i) => self.move_position(i),
Message::Quit => self.quit(),
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions exercises/functions/functions1.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// functions1.rs
// Make me compile! Execute `rustlings hint functions1` for hints :)

// I AM NOT DONE

fn main() {
call_me();
}

fn call_me() {}
3 changes: 1 addition & 2 deletions exercises/functions/functions2.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// functions2.rs
// Make me compile! Execute `rustlings hint functions2` for hints :)

// I AM NOT DONE

fn main() {
call_me(3);
}

fn call_me(num:) {
fn call_me(num: i32) {
for i in 0..num {
println!("Ring! Call number {}", i + 1);
}
Expand Down
3 changes: 1 addition & 2 deletions exercises/functions/functions3.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// functions3.rs
// Make me compile! Execute `rustlings hint functions3` for hints :)

// I AM NOT DONE

fn main() {
call_me();
call_me(0);
}

fn call_me(num: u32) {
Expand Down
3 changes: 1 addition & 2 deletions exercises/functions/functions4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
// This store is having a sale where if the price is an even number, you get
// 10 Rustbucks off, but if it's an odd number, it's 3 Rustbucks off.

// I AM NOT DONE

fn main() {
let original_price = 51;
println!("Your sale price is {}", sale_price(original_price));
}

fn sale_price(price: i32) -> {
fn sale_price(price: i32) -> i32 {
if is_even(price) {
price - 10
} else {
Expand Down
3 changes: 1 addition & 2 deletions exercises/functions/functions5.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// functions5.rs
// Make me compile! Execute `rustlings hint functions5` for hints :)

// I AM NOT DONE

fn main() {
let answer = square(3);
println!("The answer is {}", answer);
}

fn square(num: i32) -> i32 {
num * num;
num * num
}
2 changes: 1 addition & 1 deletion exercises/if/if1.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// if1.rs

// I AM NOT DONE

pub fn bigger(a: i32, b: i32) -> i32 {
// Complete this function to return the bigger number!
// Do not use:
// - another function call
// - additional variables
// Execute `rustlings hint if1` for hints
if a > b {a} else {b}
}

// Don't mind this for now :)
Expand Down
5 changes: 3 additions & 2 deletions exercises/if/if2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
// Step 2: Get the bar_for_fuzz and default_to_baz tests passing!
// Execute the command `rustlings hint if2` if you want a hint :)

// I AM NOT DONE

pub fn fizz_if_foo(fizzish: &str) -> &str {
if fizzish == "fizz" {
"foo"
} else if fizzish == "fuzz"{
"bar"
} else {
1
"baz"
}
}

Expand Down
3 changes: 1 addition & 2 deletions exercises/move_semantics/move_semantics1.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// move_semantics1.rs
// Make me compile! Execute `rustlings hint move_semantics1` for hints :)

// I AM NOT DONE

fn main() {
let vec0 = Vec::new();

let vec1 = fill_vec(vec0);
let mut vec1 = fill_vec(vec0);

println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);

Expand Down
3 changes: 1 addition & 2 deletions exercises/move_semantics/move_semantics2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
// Make me compile without changing line 13!
// Execute `rustlings hint move_semantics2` for hints :)

// I AM NOT DONE

fn main() {
let vec0 = Vec::new();

let mut vec1 = fill_vec(vec0);
let mut vec1 = fill_vec(vec0.clone());

// Do not change the following line!
println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0);
Expand Down
3 changes: 1 addition & 2 deletions exercises/move_semantics/move_semantics3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// (no lines with multiple semicolons necessary!)
// Execute `rustlings hint move_semantics3` for hints :)

// I AM NOT DONE

fn main() {
let vec0 = Vec::new();
Expand All @@ -17,7 +16,7 @@ fn main() {
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
}

fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
fn fill_vec(mut vec: Vec<i32>) -> Vec<i32> {
vec.push(22);
vec.push(44);
vec.push(66);
Expand Down
7 changes: 3 additions & 4 deletions exercises/move_semantics/move_semantics4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
// freshly created vector from fill_vec to its caller.
// Execute `rustlings hint move_semantics4` for hints!

// I AM NOT DONE

fn main() {
let vec0 = Vec::new();

let mut vec1 = fill_vec(vec0);
let mut vec1 = fill_vec();

println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);

Expand All @@ -20,7 +18,8 @@ fn main() {

// `fill_vec()` no longer takes `vec: Vec<i32>` as argument
fn fill_vec() -> Vec<i32> {
let mut vec = vec;
let vec0 = Vec::new();
let mut vec = vec0;

vec.push(22);
vec.push(44);
Expand Down
3 changes: 1 addition & 2 deletions exercises/move_semantics/move_semantics5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
// adding, changing or removing any of them.
// Execute `rustlings hint move_semantics5` for hints :)

// I AM NOT DONE

fn main() {
let mut x = 100;
let y = &mut x;
let z = &mut x;
*y += 100;
let z = &mut x;
*z += 1000;
assert_eq!(x, 1200);
}
3 changes: 1 addition & 2 deletions exercises/primitive_types/primitive_types1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Fill in the rest of the line that has code missing!
// No hints, there's no tricks, just get used to typing these :)

// I AM NOT DONE

fn main() {
// Booleans (`bool`)
Expand All @@ -12,7 +11,7 @@ fn main() {
println!("Good morning!");
}

let // Finish the rest of this line like the example! Or make it be false!
let is_evening = false;// Finish the rest of this line like the example! Or make it be false!
if is_evening {
println!("Good evening!");
}
Expand Down
3 changes: 1 addition & 2 deletions exercises/primitive_types/primitive_types2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Fill in the rest of the line that has code missing!
// No hints, there's no tricks, just get used to typing these :)

// I AM NOT DONE

fn main() {
// Characters (`char`)
Expand All @@ -16,7 +15,7 @@ fn main() {
println!("Neither alphabetic nor numeric!");
}

let // Finish this line like the example! What's your favorite character?
let your_character = 'Ö'; // Finish this line like the example! What's your favorite character?
// Try a letter, try a number, try a special character, try a character
// from a different language than your own, try an emoji!
if your_character.is_alphabetic() {
Expand Down
3 changes: 1 addition & 2 deletions exercises/primitive_types/primitive_types3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
// Create an array with at least 100 elements in it where the ??? is.
// Execute `rustlings hint primitive_types3` for hints!

// I AM NOT DONE

fn main() {
let a = ???
let a = [0;100];

if a.len() >= 100 {
println!("Wow, that's a big array!");
Expand Down
3 changes: 1 addition & 2 deletions exercises/primitive_types/primitive_types4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
// Get a slice out of Array a where the ??? is so that the test passes.
// Execute `rustlings hint primitive_types4` for hints!!

// I AM NOT DONE

#[test]
fn slice_out_of_array() {
let a = [1, 2, 3, 4, 5];

let nice_slice = ???
let nice_slice = &a[1..4];

assert_eq!([2, 3, 4], nice_slice)
}
3 changes: 1 addition & 2 deletions exercises/primitive_types/primitive_types5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
// Destructure the `cat` tuple so that the println will work.
// Execute `rustlings hint primitive_types5` for hints!

// I AM NOT DONE

fn main() {
let cat = ("Furry McFurson", 3.5);
let /* your pattern here */ = cat;
let (name, age) = cat;

println!("{} is {} years old.", name, age);
}
3 changes: 1 addition & 2 deletions exercises/primitive_types/primitive_types6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
// You can put the expression for the second element where ??? is so that the test passes.
// Execute `rustlings hint primitive_types6` for hints!

// I AM NOT DONE

#[test]
fn indexing_tuple() {
let numbers = (1, 2, 3);
// Replace below ??? with the tuple indexing syntax.
let second = ???;
let second = numbers.1;

assert_eq!(2, second,
"This is not the 2nd number in the tuple!")
Expand Down
9 changes: 7 additions & 2 deletions exercises/quiz1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
// more than 40 at once, each apple only costs 1! Write a function that calculates
// the price of an order of apples given the quantity bought. No hints this time!

// I AM NOT DONE

// Put your function here!
// fn calculate_apple_price {
fn calculate_apple_price(apples: u32) -> u32 {
if apples > 40 {
return apples
} else {
return apples*2
}
}

// Don't modify this function!
#[test]
Expand Down
12 changes: 6 additions & 6 deletions exercises/structs/structs1.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// structs1.rs
// Address all the TODOs to make the tests pass!

// I AM NOT DONE

struct ColorClassicStruct {
// TODO: Something goes here
name: String,
hex: String,
}

struct ColorTupleStruct(/* TODO: Something goes here */);
struct ColorTupleStruct(String, String);

#[derive(Debug)]
struct UnitStruct;
Expand All @@ -19,7 +19,7 @@ mod tests {
#[test]
fn classic_c_structs() {
// TODO: Instantiate a classic c struct!
// let green =
let green = ColorClassicStruct{name: String::from("green"), hex: String::from("#00FF00")};

assert_eq!(green.name, "green");
assert_eq!(green.hex, "#00FF00");
Expand All @@ -28,7 +28,7 @@ mod tests {
#[test]
fn tuple_structs() {
// TODO: Instantiate a tuple struct!
// let green =
let green = ColorTupleStruct(String::from("green"), String::from("#00FF00"));

assert_eq!(green.0, "green");
assert_eq!(green.1, "#00FF00");
Expand All @@ -37,7 +37,7 @@ mod tests {
#[test]
fn unit_structs() {
// TODO: Instantiate a unit struct!
// let unit_struct =
let unit_struct = UnitStruct;
let message = format!("{:?}s are fun!", unit_struct);

assert_eq!(message, "UnitStructs are fun!");
Expand Down
Loading

0 comments on commit 18672fa

Please sign in to comment.