You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
1.9 KiB

## display_table
### Instructions
- Implement the std::fmt::Display trait for the structure table so the table is printed like in the [Usage](#usage) the length of each column must adjust to the longest element of the column and the element must be centered in the "cell" when possible, if the length of the element doesn't allow to center exactly it must alight slightly to the right.
- Note: If the table is empty `println!` must not print anything.
- Define the associated function `new` that create a new empty table.
- Define the method function `add_row` that adds a new row to the table created from a slice of strings.
### Expected function
```rust
pub struct Table {
pub headers: Vec<String>,
pub body: Vec<Vec<String>>,
}
impl fmt::Display for Table {
}
impl Table {
pub fn new() -> Table {
}
pub fn add_row(&mut self, row: &[String]) {
}
}
```
### Usage
Here is a possible test for your function:
```rust
fn main() {
let mut table = Table::new();
println!("{}", table);
table.headers = vec![
String::from("Model"),
String::from("Piece N°"),
String::from("In Stock"),
String::from("Description"),
];
table.add_row(&[
String::from("model 1"),
String::from("43-EWQE304"),
String::from("30"),
String::from("Piece for x"),
]);
table.add_row(&[
String::from("model 2"),
String::from("98-QCVX5433"),
String::from("100000000"),
String::from("-"),
]);
table.add_row(&[
String::from("model y"),
String::from("78-NMNH"),
String::from("60"),
String::from("nothing"),
]);
println!("{}", table);
}
```
And its output:
```console
student@ubuntu:~/[[ROOT]]/test$ cargo run
| Model | Piece N° | In Stock | Description |
|---------+-------------+-----------+-------------|
| model 1 | 43-EWQE304 | 30 | Piece for x |
| model 2 | 98-QCVX5433 | 100000000 | - |
| model y | 78-NMNH | 60 | nothing |
student@ubuntu:~/[[ROOT]]/test$
```