You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.5 KiB

4 years ago
## unwrap_or_expect
### Instructions
A **function** named **odd_to_even** will be given, which returns a `Result`. If there is an even value in the vector, the function will return a tuple with a string, stating the error, and a vector with the elements that caused the error.
The objective is to execute the `odd_to_even` function and handle the errors returned by it.
Create the following functions which receive a vector:
- `expect`: which returns the error adding the string "ERROR " to the beginning.
- `unwrap_or`: which in case of error, returns an empty vector.
- `unwrap_err`: which panics if the value is `Ok`, or 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 that caused the error.
4 years ago
### Expected Functions
```rust
4 years ago
pub fn odd_to_even(data: Vec<u32>) -> Result<Vec<u32>, (String, Vec<u32>)> {
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<u32>) -> Vec<u32> {
}
pub fn unwrap_or(v: Vec<u32>) -> Vec<u32> {
}
pub fn unwrap_err(v: Vec<u32>) -> (String, Vec<u32>) {
}
pub fn unwrap(v: Vec<u32>) -> Vec<u32> {
}
pub fn unwrap_or_else(v: Vec<u32>) -> Vec<u32> {
}
```
### Usage
4 years ago
Here is a program to test your function:
```rust
4 years ago
use unwrap_or_expect::*;
fn main() {
4 years ago
// // if uncommented, the below line will give an expect "ERROR "
// println!("{:?}", expect(vec![1, 3, 2, 5]));
4 years ago
println!("{:?}", unwrap_or(vec![1, 3, 2, 5]));
println!("{:?}", unwrap_or(vec![1, 3, 5]));
4 years ago
println!("{:?}", unwrap_err(vec![1, 3, 2, 5]));
4 years ago
// // if uncommented, the below line will give an unwraped error
// println!("{:?}", unwrap_err(vec![1, 3, 5]));
4 years ago
println!("{:?}", unwrap(vec![1, 3, 5]));
4 years ago
//// if uncommented, the below line will give an error
// println!("{:?}", unwrap(vec![1, 3, 2, 5]));
4 years ago
println!("{:?}", unwrap_or_else(vec![1, 3, 5]));
println!("{:?}", unwrap_or_else(vec![3, 2, 6, 5]));
}
```
And its output:
```console
[]
[2, 4, 6]
("There is a even value in the vector!", [2])
[2, 4, 6]
4 years ago
Ok([2, 4, 6])
[2, 4, 6]
4 years ago
Err(("There is a even value in the vector!", [2, 6]))
[2, 6]
$
```
### Notions
- [Error Handling](https://doc.rust-lang.org/book/ch09-00-error-handling.html)
- [Unwrap keywords](https://doc.rust-lang.org/std/?search=unwrap)