Browse Source

docs(unwrap_or_expect): rewrite the subject

Rewrite the subject completely to give more meaninful learning experience to students.
fixDevirged
mikysett 2 years ago committed by Michele
parent
commit
dcea19a5d7
  1. 83
      subjects/unwrap_or_expect/README.md

83
subjects/unwrap_or_expect/README.md

@ -2,45 +2,31 @@
### Instructions ### 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. Create a **function** named **fetch_data** which has two arguments:
- `server`: Which is a `Result` having the server url or an error message inside.
- `security_level`: Which is an `enum` defining the behavior of the function in case of errors.
The objective is to execute the `odd_to_even` function and handle the errors returned by it. The `security_level` will work as follow:
- `Unknown`: The function panics without printing any custom message.
- `High`: The function panics and prints the error message `ERROR: program stops`.
- `Medium`: The function returns the string `WARNING: check the server`.
- `Low`: The function returns the string `Not found: [SERVER_URL]`.
- `BlockServer`: The function will panic only if the `Result` value is `Ok` and the error message will be the string contained in `Ok`.
Create the following functions which receive a vector: To return from **fetch_data** you must use `expect`, `unwrap_or`, `unwrap_err`, `unwrap` and `unwrap_or_else`.
- `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.
### Expected Functions ### Expected Functions
```rust ```rust
pub fn odd_to_even(data: Vec<u32>) -> Result<Vec<u32>, (String, Vec<u32>)> { pub enum Security {
let mut a = Vec::new(); Unknown,
a.extend(data.iter().filter(|&value| value % 2 == 0)); High,
if a.len() != 0 { Medium,
return Err(("There is a even value in the vector!".to_string(), a)); Low,
} BlockServer,
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 fetch_data(server: Result<String, String>, security_level: Security) -> String {
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> {
} }
``` ```
@ -53,38 +39,27 @@ Here is a program to test your function:
use unwrap_or_expect::*; use unwrap_or_expect::*;
fn main() { fn main() {
// // if uncommented, the below line will give an expect "ERROR " println!("{}", fetch_data(Ok("server1.com".to_string()), Security::Medium));
// println!("{:?}", expect(vec![1, 3, 2, 5])); println!("{}", fetch_data(Err(String::new()), Security::Medium));
println!("{}", fetch_data(Err("server2.com".to_string()), Security::Low));
println!("{:?}", unwrap_or(vec![1, 3, 2, 5]));
println!("{:?}", unwrap_or(vec![1, 3, 5]));
println!("{:?}", unwrap_err(vec![1, 3, 2, 5]));
// // if uncommented, the below line will give an unwraped error
// println!("{:?}", unwrap_err(vec![1, 3, 5]));
println!("{:?}", unwrap(vec![1, 3, 5])); // Panics with no custom message
// fetch_data(Err("ERROR CRITICAL".to_string()), Security::Unknown);
//// if uncommented, the below line will give an error // Panics with the message "ERROR: program stops"
// println!("{:?}", unwrap(vec![1, 3, 2, 5])); // fetch_data(Err(String::new()), Security::High);
println!("{:?}", unwrap_or_else(vec![1, 3, 5])); // Panics with the message "malicious_server.com"
println!("{:?}", unwrap_or_else(vec![3, 2, 6, 5])); // fetch_data(Ok("malicious_server.com".to_string()), Security::BlockServer);
} }
``` ```
And its output: And its output:
```console ```console
[] server1.com
[2, 4, 6] WARNING: check the server
("There is a even value in the vector!", [2]) Not found: server2.com
[2, 4, 6]
Ok([2, 4, 6])
[2, 4, 6]
Err(("There is a even value in the vector!", [2, 6]))
[2, 6]
$ $
``` ```

Loading…
Cancel
Save