diff --git a/src/context.rs b/src/context.rs index b6899f8..208dc79 100644 --- a/src/context.rs +++ b/src/context.rs @@ -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, } @@ -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, } } } diff --git a/src/exercises.rs b/src/exercises.rs index 6d19d92..2b9843f 100644 --- a/src/exercises.rs +++ b/src/exercises.rs @@ -4,4 +4,5 @@ pub mod exercise_02; pub mod exercise_03; pub mod exercise_04; pub mod exercise_05; -pub mod exercise_06; \ No newline at end of file +pub mod exercise_06; +pub mod exercise_07; \ No newline at end of file diff --git a/src/exercises/exercise_06.rs b/src/exercises/exercise_06.rs index b9925b8..cd92ab5 100644 --- a/src/exercises/exercise_06.rs +++ b/src/exercises/exercise_06.rs @@ -15,13 +15,13 @@ pub fn Component() -> impl IntoView { view! {
-

Learning by Structs and Operator Overloading

+

Learning by Functions

- {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."#}

-

Step 1: Define the Vec struct

+

Step 1: Define a Function without Return

 impl IntoView {
                 
-

Step 2: Implement Add for Vec

+

Step 2: Define a Function with Return

 impl IntoView {
                 
-

Step 3: Declare v1

+

Step 3: Call Your Functions

 impl IntoView {
                 
-

Step 4: Declare v2

+

Step 4: Call Your Greet Functions

 impl IntoView {
                         {exercise_06[3]}
                     
- -
-

Step 5: Add v1 and v2

-
-                        
-                            {exercise_06[4]}
-                        
-                    
-
-
-

Step : Print v1, v2, and v3

-
-                        
-                            {move || String::from(exercise_06[5])}
-                        
-                    
-
} diff --git a/src/exercises/exercise_07.rs b/src/exercises/exercise_07.rs new file mode 100644 index 0000000..ad0e935 --- /dev/null +++ b/src/exercises/exercise_07.rs @@ -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::(); + let InputRef(_ref) = expect_context::(); + let Exercises { exercise_07, .. } = expect_context::(); + let re = Regex::new(r"\d+\.").unwrap(); + let exercise_07 = Rc::new(re.split(exercise_07).collect::>()); + + view! { +
+
+

Learning by Structs and Operator Overloading

+

+ {r#"In this exercise, we will define a Vec struct with x and y fields and implement the Add operator for it."#} +

+ +
+

Step 1: Define the Vec struct

+
+                        {exercise_07[0]}
+                    
+
+ +
+

Step 2: Implement Add for Vec

+
+                        {exercise_07[1]}
+                    
+
+ +
+

Step 3: Declare v1

+
+                        {exercise_07[2]}
+                    
+
+ +
+

Step 4: Declare v2

+
+                        {exercise_07[3]}
+                    
+
+ +
+

Step 5: Add v1 and v2

+
+                        
+                            {exercise_07[4]}
+                        
+                    
+
+
+

Step : Print v1, v2, and v3

+
+                        
+                            {move || String::from(exercise_07[5])}
+                        
+                    
+
+
+
+ } +} diff --git a/src/instruction.rs b/src/instruction.rs index 1e489c0..178c6b9 100644 --- a/src/instruction.rs +++ b/src/instruction.rs @@ -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] @@ -31,6 +31,9 @@ pub fn Component() -> impl IntoView { + + + diff --git a/src/terminal.rs b/src/terminal.rs index 429df3f..d5b5aa6 100644 --- a/src/terminal.rs +++ b/src/terminal.rs @@ -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::>(); - 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::>(); + let exercise_07 = exercise_07[5]; + data.update(|prev| { prev.insert((prev.len(), TerminalEvent::Code), code.clone()); @@ -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); } } }