mirror of https://github.com/01-edu/public.git
docs(box_recursion): change functions and arguments names
This commit is contained in:
parent
61c1fd1ac1
commit
0c9296dbcf
|
@ -5,9 +5,9 @@
|
|||
Using the given code, create the following **associated functions**:
|
||||
|
||||
- `new`: which will initialize the `WorkEnvironment` with `grade` set to `None`.
|
||||
- `add_worker`: which receives two strings, one being the type of worker and the other the name of the worker.
|
||||
- `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.
|
||||
- `remove_worker`: which removes the last worker that was placed in the `WorkEnvironment`, this function returns an `Option` with the name of the worker.
|
||||
- `search_worker`: which returns a tuple with the name and type of worker.
|
||||
- `last_worker`: which returns an `Option` with a tuple containing the name and role of the last added worker.
|
||||
|
||||
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.
|
||||
|
||||
|
@ -21,19 +21,17 @@ pub struct WorkEnvironment {
|
|||
pub type Link =
|
||||
|
||||
pub struct Worker {
|
||||
pub worker_type: String,
|
||||
pub worker_name: String,
|
||||
pub next_worker: Link,
|
||||
pub role: String,
|
||||
pub name: String,
|
||||
pub next: Link,
|
||||
}
|
||||
|
||||
impl WorkEnvironment {
|
||||
pub fn new() -> WorkEnvironment {}
|
||||
pub fn add_worker(&mut self, t: String, name: String) {}
|
||||
pub fn add_worker(&mut self, role: String, name: String) {}
|
||||
pub fn remove_worker(&mut self) -> Option<String> {}
|
||||
pub fn search_worker(&self) -> Option<(String, String)> {}
|
||||
pub fn last_worker(&self) -> Option<(String, String)> {}
|
||||
}
|
||||
|
||||
|
||||
```
|
||||
|
||||
### Usage
|
||||
|
@ -49,13 +47,14 @@ fn main() {
|
|||
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);
|
||||
|
||||
println!("{:?}", list.last_worker());
|
||||
|
||||
list.remove_worker();
|
||||
list.remove_worker();
|
||||
list.remove_worker();
|
||||
println!("{:?}", list);
|
||||
|
||||
println!("{:?}", list.search_worker());
|
||||
|
||||
list.remove_worker();
|
||||
list.remove_worker();
|
||||
list.remove_worker();
|
||||
list.remove_worker();
|
||||
println!("{:?}", list);
|
||||
}
|
||||
|
@ -65,13 +64,40 @@ And its output:
|
|||
|
||||
```console
|
||||
$ cargo run
|
||||
WorkEnvironment { grade: Some(Worker { worker_type: "Normal Worker", worker_name: "Alice", next_worker: Some(Worker { worker_type: "Normal Worker", worker_name: "Ana", next_worker: Some(Worker { worker_type: "Manager", worker_name: "Monica", next_worker: Some(Worker { worker_type: "CEO", worker_name: "Marie", next_worker: None }) }) }) }) }
|
||||
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,
|
||||
},
|
||||
),
|
||||
},
|
||||
),
|
||||
},
|
||||
),
|
||||
},
|
||||
),
|
||||
}
|
||||
Some(("Alice", "Normal Worker"))
|
||||
WorkEnvironment { grade: Some(Worker { role: "CEO", name: "Marie", next: None }) }
|
||||
WorkEnvironment { grade: None }
|
||||
$
|
||||
```
|
||||
|
||||
### Notions
|
||||
|
||||
- [boc](https://doc.rust-lang.org/book/ch15-01-box.html)
|
||||
- [Box\<T\>](https://doc.rust-lang.org/book/ch15-01-box.html)
|
||||
- [Module std::option](https://doc.rust-lang.org/std/option/)
|
||||
|
|
Loading…
Reference in New Issue