Browse Source

docs(get_document_id): make main function consistent with instructions

DEV-4017-prototypes-exercise-1-animals
nprimo 2 years ago committed by Niccolò Primo
parent
commit
6e10678bbc
  1. 27
      subjects/get_document_id/README.md

27
subjects/get_document_id/README.md

@ -11,7 +11,7 @@ Create the following structures which will help you get the `document_id` of the
`ErrorOffice` is an `enum` with `OfficeClosed(u32)`, `OfficeNotFound(u32)` or `OfficeFull(u32)`.
The `usize` is the `id` of the office generating the error.
The `u32` is the `id` of the office generating the error.
Beside the structures, you must create a **function** named `get_document_id`, and associate it to the `OfficeOne` structure.
@ -30,16 +30,19 @@ pub enum ErrorOffice {
#[derive(PartialEq, Eq, Clone, Copy)]
pub struct OfficeOne {
pub next_office: Result<OfficeTwo, ErrorOffice>,
pub id: u32,
}
#[derive(PartialEq, Eq, Clone, Copy)]
pub struct OfficeTwo {
pub next_office: Result<OfficeThree, ErrorOffice>,
pub id: u32,
}
#[derive(PartialEq, Eq, Clone, Copy)]
pub struct OfficeThree {
pub next_office: Result<OfficeFour, ErrorOffice>,
pub id: u32,
}
#[derive(PartialEq, Eq, Clone, Copy)]
@ -65,32 +68,38 @@ fn main() {
next_office: Ok(OfficeThree {
next_office: Ok(OfficeFour {
document_id: Ok(13),
id: 4,
}),
id: 3,
}),
id: 2,
}),
id: 1,
};
let office_closed = {
OfficeOne {
next_office: Ok(OfficeTwo {
next_office: Err(ErrorOffice::OfficeClose(13)),
next_office: Err(ErrorOffice::OfficeClose(2)),
id: 2,
}),
id: 1,
}
};
match office_ok.get_document_id() {
Ok(id) => println!("Found a document with id {}", id),
Err(err) => match err {
ErrorOffice::OfficeClose(_) => println!("Error: office closed!"),
ErrorOffice::OfficeNotFound(_) => println!("Error: office not found!"),
ErrorOffice::OfficeFull(_) => println!("Error: office full!"),
ErrorOffice::OfficeClose(id) => println!("Error: office {id} closed!"),
ErrorOffice::OfficeNotFound(id) => println!("Error: office {id} not found!"),
ErrorOffice::OfficeFull(id) => println!("Error: office {id} full!"),
},
};
match office_closed.get_document_id() {
Ok(id) => println!("Found a document with id {}", id),
Err(err) => match err {
ErrorOffice::OfficeClose(_) => println!("Error: office closed!"),
ErrorOffice::OfficeNotFound(_) => println!("Error: office not found!"),
ErrorOffice::OfficeFull(_) => println!("Error: office full!"),
ErrorOffice::OfficeClose(id) => println!("Error: office {id} closed!"),
ErrorOffice::OfficeotFound(id) => println!("Error: office {id} not found!"),
ErrorOffice::OfficeFull(id) => println!("Error: office {id} full!"),
},
};
}
@ -101,6 +110,6 @@ And its output:
```console
$ cargo run
Found a document with id 13
Error: office closed!
Error: office 2 closed!
$
```

Loading…
Cancel
Save