From 8eb08edf9b35d631c8b583dcea439f17e47426a7 Mon Sep 17 00:00:00 2001 From: Ramaguru Radhakrishnan <7790256+ramagururadhakrishnan@users.noreply.github.com> Date: Wed, 8 May 2024 10:15:14 +0530 Subject: [PATCH] Result Enum Example Added --- Assets/Lectures/RL5.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Assets/Lectures/RL5.md b/Assets/Lectures/RL5.md index 5d73182..6789fe2 100644 --- a/Assets/Lectures/RL5.md +++ b/Assets/Lectures/RL5.md @@ -5,6 +5,7 @@ ## Lecture 5 - Misc ![](https://img.shields.io/badge/-8th_May-orange) +### Match ``` fn main() { let number = 13; @@ -36,3 +37,24 @@ fn main() { println!("{} -> {}", boolean, binary); } ``` +### Error - Result Enum +``` +fn main() { + // Example of using Result to handle division by zero error + match divide(10.0, 0.0) { + Ok(result) => println!("Result of division: {}", result), + Err(err) => println!("Error: {}", err), + } +} + +// Function to divide two numbers +fn divide(x: f64, y: f64) -> Result { + if y == 0.0 { + Err("Division by zero") + } else { + Ok(x / y) + } +} +``` + +