File: concat.rs

package info (click to toggle)
rustc-web 1.78.0%2Bdfsg1-2~deb11u3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,245,360 kB
  • sloc: xml: 147,985; javascript: 18,022; sh: 11,083; python: 10,265; ansic: 6,172; cpp: 5,023; asm: 4,390; makefile: 4,269
file content (58 lines) | stat: -rw-r--r-- 1,473 bytes parent folder | download | duplicates (16)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//! This example demonstrates using the [`Concat`] [`TableOption`] to concatenate
//! [`tables`](Table) together.
//!
//! * [`Concat`] supports appending tables vertically and horizontally.
//!
//! * Note how the base tables style settings take take precedence over the appended table.
//! If the two tables are of unequal shape, additional blank cells are added as needed.

use tabled::{
    settings::{object::Segment, Alignment, Concat, Modify, Style},
    Table, Tabled,
};

#[derive(Debug, Tabled)]
struct Weather {
    temperature_c: f64,
    wind_ms: f64,
}

#[derive(Debug, Tabled)]
struct Location(
    #[tabled(rename = "latitude")] f64,
    #[tabled(rename = "longitude")] f64,
);

fn main() {
    let weather_data = [
        Weather {
            temperature_c: 1.0,
            wind_ms: 3.0,
        },
        Weather {
            temperature_c: -20.0,
            wind_ms: 30.0,
        },
        Weather {
            temperature_c: 40.0,
            wind_ms: 100.0,
        },
    ];

    let location_data = [
        Location(111.111, 333.333),
        Location(5.111, 7282.1),
        Location(0.0, 0.0),
        Location(0.0, 0.0),
    ];

    let location_table = Table::new(location_data);

    let mut weather_table = Table::new(weather_data);
    weather_table
        .with(Concat::horizontal(location_table))
        .with(Style::empty())
        .with(Modify::new(Segment::all()).with(Alignment::left()));

    println!("{weather_table}");
}