]> git.proxmox.com Git - rustc.git/blob - vendor/tabled/examples/builder_index.rs
New upstream version 1.75.0+dfsg1
[rustc.git] / vendor / tabled / examples / builder_index.rs
1 //! This example demonstrates evolving the standard [`Builder`] to an [`IndexBuilder`],
2 //! and then manipulating the constructing table with a newly prepended index column.
3 //!
4 //! * An [`IndexBuilder`] is capable of several useful manipulations, including:
5 //! * Giving the new index column a name
6 //! * Transposing the index column around a table
7 //! * Choosing a location for the new index column besides 0; the default
8 //!
9 //! * Note that like with any builder pattern the [`IndexBuilder::build()`] function
10 //! is necessary to produce a displayable [`Table`].
11
12 use tabled::{settings::Style, Table, Tabled};
13
14 #[derive(Tabled)]
15 struct Distribution {
16 name: &'static str,
17 based_on: &'static str,
18 is_active: bool,
19 is_cool: bool,
20 }
21
22 impl Distribution {
23 fn new(name: &'static str, based_on: &'static str, is_active: bool, is_cool: bool) -> Self {
24 Self {
25 name,
26 based_on,
27 is_active,
28 is_cool,
29 }
30 }
31 }
32
33 fn main() {
34 let data = [
35 Distribution::new("Manjaro", "Arch", true, true),
36 Distribution::new("Arch", "None", true, true),
37 Distribution::new("Debian", "None", true, true),
38 ];
39
40 let mut table = Table::builder(data)
41 .index()
42 .column(0)
43 .name(None)
44 .transpose()
45 .build();
46
47 table.with(Style::modern());
48
49 println!("{table}");
50 }