From e10771014a554d391ae58dd19fdbf4c796f157f8 Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 11 Feb 2021 19:25:39 +0000 Subject: [PATCH] corrections --- subjects/unwrap_or_expect/README.md | 84 +++++++++++++++++++---------- 1 file changed, 57 insertions(+), 27 deletions(-) diff --git a/subjects/unwrap_or_expect/README.md b/subjects/unwrap_or_expect/README.md index 249a3caa..d9dd14a4 100644 --- a/subjects/unwrap_or_expect/README.md +++ b/subjects/unwrap_or_expect/README.md @@ -1,48 +1,81 @@ -## unwrap +## unwrap_or_expect ### Instructions -It will be given a function called **odd_to_even**, that returns an `Result`. If its an error it will -return a tuple with a string, indicating the error, and a vector with the elements that justifies the error +A **function** called **odd_to_even** will be given, which returns a `Result`. If an error occurs the function will +return a tuple with a string, stating the error, and a vector with the elements which causing the error. -The objective is to execute the `odd_to_even` function and handle the error given by it +The objective is to execute the `odd_to_even` function and handle the error returned by it. -Create the following functions that receives a vector : +Create the following functions which receives a vector : -- `expect` that returns the error adding the sting "ERROR " -- `unwrap_or` that in case of error returns an empty vector -- `unwrap_err` that returns error if its `Ok` and returns the - string containing the error in case of `Err` -- `unwrap` that unwraps the `Result` -- `unwrap_or_else` that in case of error returns a the vector that justifies the error +- `expect` which returns the error adding the string "ERROR " +- `unwrap_or` which in case of error returns an empty vector +- `unwrap_err` which returns error if its `Ok` and returns the + string containing the error in case of `Err` +- `unwrap` which unwraps the `Result` +- `unwrap_or_else` which in case of error returns the vector of elements which causes the error -### Expected Function +### Notions + +- [Error Handling](https://doc.rust-lang.org/book/ch09-00-error-handling.html) +- [Unwrap keywords](https://doc.rust-lang.org/std/?search=unwrap) + +### Expected Functions ```rust -pub fn odd_to_even(data: Vec) -> Result, (String, Vec)> {} -pub fn expect(v: Vec) -> Vec {} -pub fn unwrap_or(v: Vec) -> Vec {} -pub fn unwrap_err(v: Vec) -> (String, Vec) {} -pub fn unwrap(v: Vec) -> Vec {} -pub fn unwrap_or_else(v: Vec) -> Vec {} +pub fn odd_to_even(data: Vec) -> Result, (String, Vec)> { + let mut a = Vec::new(); + a.extend(data.iter().filter(|&value| value % 2 == 0)); + if a.len() != 0 { + return Err(("There is a even value in the vector!".to_string(), a)); + } + a.extend(data.iter().map(|&value| { + value + 1 + })); + Ok(a) +} +pub fn expect(v: Vec) -> Vec { + +} +pub fn unwrap_or(v: Vec) -> Vec { + +} +pub fn unwrap_err(v: Vec) -> (String, Vec) { + +} +pub fn unwrap(v: Vec) -> Vec { + +} +pub fn unwrap_or_else(v: Vec) -> Vec { + +} ``` ### Usage -Here is a program to test your function +Here is a program to test your function: ```rust +use unwrap_or_expect::*; + fn main() { - // this will give an expect error + // // if uncommented, the below line will give an expect "ERROR " // println!("{:?}", expect(vec![1, 3, 2, 5])); + println!("{:?}", unwrap_or(vec![1, 3, 2, 5])); println!("{:?}", unwrap_or(vec![1, 3, 5])); + println!("{:?}", unwrap_err(vec![1, 3, 2, 5])); - // this will give an error that is unwraped + + // // if uncommented, the below line will give an unwraped error // println!("{:?}", unwrap_err(vec![1, 3, 5])); + println!("{:?}", unwrap(vec![1, 3, 5])); - // this will give an error + + //// if uncommented, the below line will give an error // println!("{:?}", unwrap(vec![1, 3, 2, 5])); + println!("{:?}", unwrap_or_else(vec![1, 3, 5])); println!("{:?}", unwrap_or_else(vec![3, 2, 6, 5])); } @@ -51,16 +84,13 @@ fn main() { And its output: ```console -student@ubuntu:~/[[ROOT]]/test$ cargo run [] [2, 4, 6] ("There is a even value in the vector!", [2]) [2, 4, 6] +Ok([2, 4, 6]) [2, 4, 6] +Err(("There is a even value in the vector!", [2, 6])) [2, 6] student@ubuntu:~/[[ROOT]]/test$ ``` - -### Notions - -- https://doc.rust-lang.org/std/?search=unwrap