Skip to content

Commit

Permalink
Merge pull request #5 from rust-dd/feature/new_exercise_07
Browse files Browse the repository at this point in the history
feat: add new exercise
  • Loading branch information
dancixx authored Aug 28, 2024
2 parents bee395b + d3cdf1c commit 0ada288
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 54 deletions.
6 changes: 4 additions & 2 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ where
pub exercise_04: &'a str,
pub exercise_05: &'a str,
pub exercise_06: &'a str,
pub exercise_07: &'a str,
pub count: usize,
}

Expand All @@ -31,8 +32,9 @@ impl<'a> Default for Exercises<'a> {
exercise_03: r#"let mut x = 10;1.x = 15;2.println!("The value of x is: {}", x);"#,
exercise_04: r#"let mut y = 20;1.let y_ref1 = &y;2.let y_ref2 = &y;3.println!("y_ref1: {}, y_ref2: {}", y_ref1, y_ref2);4.let y_mut_ref = &mut y;5.println!("y_mut_ref: {}", y_mut_ref);"#,
exercise_05: r#"let tuple = (10, 20);1.println!("The elements are: {} {}", tuple.0, tuple.1);2.let (x, y) = tuple;3.println!("Destructured: x = {}, y = {}", x, y);"#,
exercise_06: r#"#[derive(Debug)] struct Vec { x: i32, y: i32 };1.impl std::ops::Add for Vec { type Output = Self; fn add(self, other: Self) -> Self { Self { x: self.x + other.x, y: self.y + other.y } } };2.let v1 = Vec { x: 1, y: 2 };3.let v2 = Vec { x: 3, y: 4 };4.let result = v1 + v2;5.println!("result: {:?}", result);"#,
count: 6,
exercise_06: r#"fn greet() { println!("Hi there!"); };1.fn dice_roll() -> i32 { 4 };2.greet();3.let result = dice_roll(); println!("Dice roll result: {}", result);"#,
exercise_07: r#"#[derive(Debug)] struct Vec { x: i32, y: i32 };1.impl std::ops::Add for Vec { type Output = Self; fn add(self, other: Self) -> Self { Self { x: self.x + other.x, y: self.y + other.y } } };2.let v1 = Vec { x: 1, y: 2 };3.let v2 = Vec { x: 3, y: 4 };4.let result = v1 + v2;5.println!("result: {:?}", result);"#,
count: 7,
}
}
}
3 changes: 2 additions & 1 deletion src/exercises.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ pub mod exercise_02;
pub mod exercise_03;
pub mod exercise_04;
pub mod exercise_05;
pub mod exercise_06;
pub mod exercise_06;
pub mod exercise_07;
55 changes: 6 additions & 49 deletions src/exercises/exercise_06.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ pub fn Component() -> impl IntoView {
view! {
<div class="flex flex-col gap-4 w-full text-white rounded-lg">
<div class="flex flex-col gap-2">
<h2 class="text-3xl font-bold">Learning by Structs and Operator Overloading</h2>
<h2 class="text-3xl font-bold">Learning by Functions</h2>
<p class="text-gray-400">
{r#"In this exercise, we will define a Vec struct with x and y fields and implement the Add operator for it."#}
{r#"In this exercise, we'll define and use functions in Rust."#}
</p>

<div class="flex flex-col gap-1 mt-2">
<h3 class="text-xl font-bold">Step 1: Define the Vec struct</h3>
<h3 class="text-xl font-bold">Step 1: Define a Function without Return</h3>
<pre
on:click={
let exercise_06 = exercise_06.clone();
Expand All @@ -41,7 +41,7 @@ pub fn Component() -> impl IntoView {
</div>

<div class="flex flex-col gap-1">
<h3 class="text-xl font-bold">Step 2: Implement Add for Vec</h3>
<h3 class="text-xl font-bold">Step 2: Define a Function with Return</h3>
<pre
on:click={
let exercise_06 = exercise_06.clone();
Expand All @@ -61,7 +61,7 @@ pub fn Component() -> impl IntoView {
</div>

<div class="flex flex-col gap-1">
<h3 class="text-xl font-bold">Step 3: Declare v1</h3>
<h3 class="text-xl font-bold">Step 3: Call Your Functions</h3>
<pre
on:click={
let exercise_06 = exercise_06.clone();
Expand All @@ -81,7 +81,7 @@ pub fn Component() -> impl IntoView {
</div>

<div class="flex flex-col gap-1">
<h3 class="text-xl font-bold">Step 4: Declare v2</h3>
<h3 class="text-xl font-bold">Step 4: Call Your Greet Functions</h3>
<pre
on:click={
let exercise_06 = exercise_06.clone();
Expand All @@ -99,49 +99,6 @@ pub fn Component() -> impl IntoView {
<code class="font-mono text-sm text-yellow-500">{exercise_06[3]}</code>
</pre>
</div>

<div class="flex flex-col gap-1">
<h3 class="text-xl font-bold">Step 5: Add v1 and v2</h3>
<pre
on:click={
let exercise_06 = exercise_06.clone();
move |_| {
set_code(String::from(exercise_06[4]));
_ref()
.get()
.expect("input_ref should be loaded by now")
.focus()
.unwrap();
}
}
class="p-4 whitespace-pre-wrap bg-black bg-opacity-30 rounded-lg transition duration-500 cursor-pointer hover:bg-black hover:bg-opacity-50"
>
<code class="font-mono text-sm text-left text-yellow-500">
{exercise_06[4]}
</code>
</pre>
</div>
<div class="flex flex-col gap-1">
<h3 class="text-xl font-bold">Step : Print v1, v2, and v3</h3>
<pre
on:click={
let exercise_06 = exercise_06.clone();
move |_| {
set_code(String::from(exercise_06[5]));
_ref()
.get()
.expect("input_ref should be loaded by now")
.focus()
.unwrap();
}
}
class="p-4 whitespace-pre-wrap bg-black bg-opacity-30 rounded-lg transition duration-500 cursor-pointer hover:bg-black hover:bg-opacity-50"
>
<code class="font-mono text-sm text-left text-yellow-500">
{move || String::from(exercise_06[5])}
</code>
</pre>
</div>
</div>
</div>
}
Expand Down
148 changes: 148 additions & 0 deletions src/exercises/exercise_07.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
use leptos::*;
use regex::Regex;
use std::rc::Rc;

use crate::context::{CodeSetter, Exercises, InputRef};

#[component]
pub fn Component() -> impl IntoView {
let CodeSetter(set_code) = expect_context::<CodeSetter>();
let InputRef(_ref) = expect_context::<InputRef>();
let Exercises { exercise_07, .. } = expect_context::<Exercises>();
let re = Regex::new(r"\d+\.").unwrap();
let exercise_07 = Rc::new(re.split(exercise_07).collect::<Vec<&str>>());

view! {
<div class="flex flex-col gap-4 w-full text-white rounded-lg">
<div class="flex flex-col gap-2">
<h2 class="text-3xl font-bold">Learning by Structs and Operator Overloading</h2>
<p class="text-gray-400">
{r#"In this exercise, we will define a Vec struct with x and y fields and implement the Add operator for it."#}
</p>

<div class="flex flex-col gap-1 mt-2">
<h3 class="text-xl font-bold">Step 1: Define the Vec struct</h3>
<pre
on:click={
let exercise_07 = exercise_07.clone();
move |_| {
set_code(String::from(exercise_07[0]));
_ref()
.get()
.expect("input_ref should be loaded by now")
.focus()
.unwrap();
}
}
class="p-4 whitespace-pre-wrap bg-black bg-opacity-30 rounded-lg transition duration-500 cursor-pointer hover:bg-black hover:bg-opacity-50"
>
<code class="font-mono text-sm text-yellow-500">{exercise_07[0]}</code>
</pre>
</div>

<div class="flex flex-col gap-1">
<h3 class="text-xl font-bold">Step 2: Implement Add for Vec</h3>
<pre
on:click={
let exercise_07 = exercise_07.clone();
move |_| {
set_code(String::from(exercise_07[1]));
_ref()
.get()
.expect("input_ref should be loaded by now")
.focus()
.unwrap();
}
}
class="p-4 whitespace-pre-wrap bg-black bg-opacity-30 rounded-lg transition duration-500 cursor-pointer hover:bg-black hover:bg-opacity-50"
>
<code class="font-mono text-sm text-yellow-500">{exercise_07[1]}</code>
</pre>
</div>

<div class="flex flex-col gap-1">
<h3 class="text-xl font-bold">Step 3: Declare v1</h3>
<pre
on:click={
let exercise_07 = exercise_07.clone();
move |_| {
set_code(String::from(exercise_07[2]));
_ref()
.get()
.expect("input_ref should be loaded by now")
.focus()
.unwrap();
}
}
class="p-4 whitespace-pre-wrap bg-black bg-opacity-30 rounded-lg transition duration-500 cursor-pointer hover:bg-black hover:bg-opacity-50"
>
<code class="font-mono text-sm text-yellow-500">{exercise_07[2]}</code>
</pre>
</div>

<div class="flex flex-col gap-1">
<h3 class="text-xl font-bold">Step 4: Declare v2</h3>
<pre
on:click={
let exercise_07 = exercise_07.clone();
move |_| {
set_code(String::from(exercise_07[3]));
_ref()
.get()
.expect("input_ref should be loaded by now")
.focus()
.unwrap();
}
}
class="p-4 whitespace-pre-wrap bg-black bg-opacity-30 rounded-lg transition duration-500 cursor-pointer hover:bg-black hover:bg-opacity-50"
>
<code class="font-mono text-sm text-yellow-500">{exercise_07[3]}</code>
</pre>
</div>

<div class="flex flex-col gap-1">
<h3 class="text-xl font-bold">Step 5: Add v1 and v2</h3>
<pre
on:click={
let exercise_07 = exercise_07.clone();
move |_| {
set_code(String::from(exercise_07[4]));
_ref()
.get()
.expect("input_ref should be loaded by now")
.focus()
.unwrap();
}
}
class="p-4 whitespace-pre-wrap bg-black bg-opacity-30 rounded-lg transition duration-500 cursor-pointer hover:bg-black hover:bg-opacity-50"
>
<code class="font-mono text-sm text-left text-yellow-500">
{exercise_07[4]}
</code>
</pre>
</div>
<div class="flex flex-col gap-1">
<h3 class="text-xl font-bold">Step : Print v1, v2, and v3</h3>
<pre
on:click={
let exercise_07 = exercise_07.clone();
move |_| {
set_code(String::from(exercise_07[5]));
_ref()
.get()
.expect("input_ref should be loaded by now")
.focus()
.unwrap();
}
}
class="p-4 whitespace-pre-wrap bg-black bg-opacity-30 rounded-lg transition duration-500 cursor-pointer hover:bg-black hover:bg-opacity-50"
>
<code class="font-mono text-sm text-left text-yellow-500">
{move || String::from(exercise_07[5])}
</code>
</pre>
</div>
</div>
</div>
}
}
5 changes: 4 additions & 1 deletion src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use leptos::*;

use crate::{
context::{Exercises, Progress},
exercises::{exercise_00, exercise_01, exercise_02, exercise_03, exercise_04, exercise_05, exercise_06},
exercises::{exercise_00, exercise_01, exercise_02, exercise_03, exercise_04, exercise_05, exercise_06, exercise_07},
};

#[component]
Expand Down Expand Up @@ -31,6 +31,9 @@ pub fn Component() -> impl IntoView {
<Show when=move || progress.get() == 5>
<exercise_06::Component />
</Show>
<Show when=move || progress.get() == 6>
<exercise_07::Component />
</Show>
<Show when=move || progress.get() == count>
<exercise_00::Component />
</Show>
Expand Down
8 changes: 7 additions & 1 deletion src/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ pub fn Component() -> impl IntoView {
let exercise_05 = exercise_05[3];
let exercise_06 = exercises.exercise_06;
let exercise_06 = re.split(exercise_06).collect::<Vec<&str>>();
let exercise_06 = exercise_06[5];
let exercise_06 = exercise_06[3];
let exercise_07 = exercises.exercise_07;
let exercise_07 = re.split(exercise_07).collect::<Vec<&str>>();
let exercise_07 = exercise_07[5];


data.update(|prev| {
prev.insert((prev.len(), TerminalEvent::Code), code.clone());
Expand Down Expand Up @@ -84,6 +88,8 @@ pub fn Component() -> impl IntoView {
progress.update(|prev| *prev += 1);
} else if progress.get() == 5 && code == *exercise_06 {
progress.update(|prev| *prev += 1);
} else if progress.get() == 6 && code == *exercise_07 {
progress.update(|prev| *prev += 1);
}
}
}
Expand Down

0 comments on commit 0ada288

Please sign in to comment.