public/subjects/box_recursion/README.md

106 lines
3.0 KiB
Markdown
Raw Permalink Normal View History

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`.
- `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.
- `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
#[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
#[derive(Debug)]
2020-12-28 17:11:42 +00:00
pub struct Worker {
pub role: String,
pub name: String,
pub next: Link,
2020-12-28 17:11:42 +00:00
}
impl WorkEnvironment {
pub fn new() -> WorkEnvironment {}
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> {}
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"));
println!("{:#?}", list);
2020-12-28 17:11:42 +00:00
println!("{:?}", list.last_worker());
2020-12-28 17:11:42 +00:00
list.remove_worker();
list.remove_worker();
list.remove_worker();
println!("{:?}", list);
2020-12-28 17:11:42 +00:00
list.remove_worker();
println!("{:?}", list);
}
```
And its output:
```console
$ cargo run
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"))
WorkEnvironment { grade: Some(Worker { role: "CEO", name: "Marie", next: None }) }
2020-12-28 17:11:42 +00:00
WorkEnvironment { grade: None }
$
2020-12-28 17:11:42 +00:00
```
2022-06-03 11:47:57 +00:00
### Notions
- [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/)