From dfd7b7e2ed25e9c99dbec40ffa6448e4cd0eebc7 Mon Sep 17 00:00:00 2001 From: mustafa Date: Sat, 18 Oct 2025 10:53:55 +0100 Subject: [PATCH 1/3] fix pattern matching exercise solution ,my solution works on more complex Expressions and Operations than the old one --- src/pattern-matching/exercise.rs | 44 ++++++-------------------------- 1 file changed, 8 insertions(+), 36 deletions(-) diff --git a/src/pattern-matching/exercise.rs b/src/pattern-matching/exercise.rs index 90fe653707af..3426002ed5a1 100644 --- a/src/pattern-matching/exercise.rs +++ b/src/pattern-matching/exercise.rs @@ -1,31 +1,13 @@ -// Copyright 2023 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#![allow(dead_code)] -// ANCHOR: solution -// ANCHOR: Operation /// An operation to perform on two subexpressions. #[derive(Debug)] +#[derive(PartialEq)] enum Operation { Add, Sub, Mul, Div, } -// ANCHOR_END: Operation -// ANCHOR: Expression /// An expression, in tree form. #[derive(Debug)] enum Expression { @@ -35,27 +17,18 @@ enum Expression { /// A literal value Value(i64), } -// ANCHOR_END: Expression -// ANCHOR: eval fn eval(e: Expression) -> i64 { - // ANCHOR_END: eval - match e { - Expression::Op { op, left, right } => { - let left = eval(*left); - let right = eval(*right); - match op { - Operation::Add => left + right, - Operation::Sub => left - right, - Operation::Mul => left * right, - Operation::Div => left / right, - } - } - Expression::Value(v) => v, + match e{ + Expression::Op{op: a, left: b, right: c} if a == Operation::Add => return eval(*b) + eval(*c), + Expression::Op{op: a, left: b, right: c} if a == Operation::Sub => return eval(*b) - eval(*c), + Expression::Op{op: a, left: b, right: c} if a == Operation::Mul => return eval(*b) * eval(*c), + Expression::Op{op: a, left: b, right: c} if a == Operation::Div => return eval(*b) / eval(*c), + Expression::Value(a) => return a, + _ => return 0, } } -// ANCHOR: tests #[test] fn test_value() { assert_eq!(eval(Expression::Value(19)), 19); @@ -138,4 +111,3 @@ fn test_div() { 5 ) } -// ANCHOR_END: tests From 1d86274534e2e44668da84d6bdb6ad0a16d9b582 Mon Sep 17 00:00:00 2001 From: Mustafa Date: Sat, 18 Oct 2025 11:36:14 +0100 Subject: [PATCH 2/3] Refactor eval function and update Operation enum --- src/pattern-matching/exercise.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/pattern-matching/exercise.rs b/src/pattern-matching/exercise.rs index 3426002ed5a1..1da2511b41b0 100644 --- a/src/pattern-matching/exercise.rs +++ b/src/pattern-matching/exercise.rs @@ -1,13 +1,31 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#![allow(dead_code)] +// ANCHOR: solution +// ANCHOR: Operation /// An operation to perform on two subexpressions. #[derive(Debug)] -#[derive(PartialEq)] enum Operation { Add, Sub, Mul, Div, } +// ANCHOR_END: Operation +// ANCHOR: Expression /// An expression, in tree form. #[derive(Debug)] enum Expression { @@ -17,8 +35,11 @@ enum Expression { /// A literal value Value(i64), } +// ANCHOR_END: Expression +// ANCHOR: eval fn eval(e: Expression) -> i64 { + // ANCHOR_END: eval match e{ Expression::Op{op: a, left: b, right: c} if a == Operation::Add => return eval(*b) + eval(*c), Expression::Op{op: a, left: b, right: c} if a == Operation::Sub => return eval(*b) - eval(*c), @@ -29,6 +50,7 @@ fn eval(e: Expression) -> i64 { } } +// ANCHOR: tests #[test] fn test_value() { assert_eq!(eval(Expression::Value(19)), 19); @@ -111,3 +133,4 @@ fn test_div() { 5 ) } +// ANCHOR_END: tests From e1bbe8b1cd9b35bc357a6323154b68c3973f529b Mon Sep 17 00:00:00 2001 From: Mustafa Date: Sat, 18 Oct 2025 11:41:18 +0100 Subject: [PATCH 3/3] Implement PartialEq for Operation enum --- src/pattern-matching/exercise.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/pattern-matching/exercise.rs b/src/pattern-matching/exercise.rs index 1da2511b41b0..fd004426343d 100644 --- a/src/pattern-matching/exercise.rs +++ b/src/pattern-matching/exercise.rs @@ -17,6 +17,7 @@ // ANCHOR: Operation /// An operation to perform on two subexpressions. #[derive(Debug)] +#[derive(PartialEq)] enum Operation { Add, Sub,