2020-12-28 17:11:42 +00:00
## box_recursion
### Instructions
2022-06-03 11:47:57 +00:00
Using the given code, create the following **associated functions** :
2020-12-28 17:11:42 +00:00
2022-06-03 11:47:57 +00:00
- `new` : which will initialize the `WorkEnvironment` with `grade` set to `None` .
2022-10-06 15:35:20 +00:00
- `add_worker` : which receives two strings, one being the role and the other the name of the worker. It will add the worker at the start of the list.
2022-06-03 11:47:57 +00:00
- `remove_worker` : which removes the last worker that was placed in the `WorkEnvironment` , this function returns an `Option` with the name of the worker.
2022-10-06 15:35:20 +00:00
- `last_worker` : which returns an `Option` with a tuple containing the name and role of the last added worker.
2020-12-28 17:11:42 +00:00
2022-06-03 11:47:57 +00:00
You must also create a type named `Link` . This will be the connection between the `WorkEnvironment` and `Worker` structures. This will be a recursion type, and it must point to `None` if there is no `Worker` to point to.
2021-03-09 03:33:19 +00:00
### Expected Functions and structures
2020-12-28 17:11:42 +00:00
```rust
2022-10-06 15:44:16 +00:00
#[derive(Debug)]
2020-12-28 17:11:42 +00:00
pub struct WorkEnvironment {
pub grade: Link,
}
2021-03-09 03:33:19 +00:00
pub type Link =
2020-12-28 17:11:42 +00:00
2022-10-06 15:44:16 +00:00
#[derive(Debug)]
2020-12-28 17:11:42 +00:00
pub struct Worker {
2022-10-06 15:35:20 +00:00
pub role: String,
pub name: String,
pub next: Link,
2020-12-28 17:11:42 +00:00
}
impl WorkEnvironment {
pub fn new() -> WorkEnvironment {}
2022-10-06 15:35:20 +00:00
pub fn add_worker(& mut self, role: String, name: String) {}
2020-12-28 17:11:42 +00:00
pub fn remove_worker(& mut self) -> Option< String > {}
2022-10-06 15:35:20 +00:00
pub fn last_worker(& self) -> Option< (String, String)> {}
2020-12-28 17:11:42 +00:00
}
```
### Usage
2021-03-09 03:33:19 +00:00
Here is a program to test your function,
2020-12-28 17:11:42 +00:00
```rust
2021-03-09 03:33:19 +00:00
use box_recursion::*;
2020-12-28 17:11:42 +00:00
fn main() {
let mut list = WorkEnvironment::new();
list.add_worker(String::from("CEO"), String::from("Marie"));
list.add_worker(String::from("Manager"), String::from("Monica"));
list.add_worker(String::from("Normal Worker"), String::from("Ana"));
list.add_worker(String::from("Normal Worker"), String::from("Alice"));
2022-10-06 15:35:20 +00:00
println!("{:#?}", list);
2020-12-28 17:11:42 +00:00
2022-10-06 15:35:20 +00:00
println!("{:?}", list.last_worker());
2020-12-28 17:11:42 +00:00
list.remove_worker();
list.remove_worker();
list.remove_worker();
2022-10-06 15:35:20 +00:00
println!("{:?}", list);
2020-12-28 17:11:42 +00:00
list.remove_worker();
println!("{:?}", list);
}
```
And its output:
```console
2021-04-28 09:47:34 +00:00
$ cargo run
2022-10-06 15:35:20 +00:00
WorkEnvironment {
grade: Some(
Worker {
role: "Normal Worker",
name: "Alice",
next: Some(
Worker {
role: "Normal Worker",
name: "Ana",
next: Some(
Worker {
role: "Manager",
name: "Monica",
next: Some(
Worker {
role: "CEO",
name: "Marie",
next: None,
},
),
},
),
},
),
},
),
}
2020-12-28 17:11:42 +00:00
Some(("Alice", "Normal Worker"))
2022-10-06 15:35:20 +00:00
WorkEnvironment { grade: Some(Worker { role: "CEO", name: "Marie", next: None }) }
2020-12-28 17:11:42 +00:00
WorkEnvironment { grade: None }
2021-04-28 09:47:34 +00:00
$
2020-12-28 17:11:42 +00:00
```
2022-06-03 11:47:57 +00:00
### Notions
2022-10-06 15:35:20 +00:00
- [Box\<T\> ](https://doc.rust-lang.org/book/ch15-01-box.html )
2022-06-03 11:47:57 +00:00
- [Module std::option ](https://doc.rust-lang.org/std/option/ )