pub struct Builder<'a> { /* private fields */ }
Expand description

Builder creates a Table from dynamic data set.

It useful when the amount of columns or rows is not known statically.

use tabled::builder::Builder;

let mut builder = Builder::default();
builder.set_columns(["index", "measure", "value"]);
builder.add_record(["0", "weight", "0.443"]);

let table = builder.build();

println!("{}", table);

It may be useful to use FromIterator for building.

use tabled::builder::Builder;
use std::iter::FromIterator;

let data = vec![
    ["column1", "column2"],
    ["data1", "data2"],
    ["data3", "data4"],
];

let table = Builder::from_iter(data).build();

println!("{}", table);

Implementations

Creates a Builder instance.

Set a column size.

If it make it lower then it was originally it is considered NOP.

Sets a Table header.

use tabled::builder::Builder;

let mut builder = Builder::default();
builder
    .set_columns((0..3).map(|i| i.to_string()))
    .add_record(["i", "surname", "lastname"]);

Sets off a Table header.

If not set its a nop.

use tabled::Table;

let data = [("Hello", 1u8, false), ("World", 21u8, true)];

let table = Table::builder(data).build().to_string();

assert_eq!(
    table,
    "+-------+----+-------+\n\
     | &str  | u8 | bool  |\n\
     +-------+----+-------+\n\
     | Hello | 1  | false |\n\
     +-------+----+-------+\n\
     | World | 21 | true  |\n\
     +-------+----+-------+"
);


let mut builder = Table::builder(data);
builder.remove_columns();
let table = builder.build().to_string();

assert_eq!(
    table,
    "+-------+----+-------+\n\
     | Hello | 1  | false |\n\
     +-------+----+-------+\n\
     | World | 21 | true  |\n\
     +-------+----+-------+"
);

Adds a row to a Table.

If Self::set_columns is not set the first row will be considered a header.

use tabled::builder::Builder;

let mut builder = Builder::default();
builder.add_record((0..3).map(|i| i.to_string()));
builder.add_record(["i", "surname", "lastname"]);

Sets a content of cells which are created in case rows has different length.

use tabled::builder::Builder;

let mut builder = Builder::default();
builder.set_default_text("undefined");
builder.set_columns((0..3).map(|i| i.to_string()));
builder.add_record(["i"]);

Build creates a Table instance.

use tabled::builder::Builder;

let mut builder = Builder::default();
builder.set_columns(["i", "column1", "column2"]);
builder.add_record(["0", "value1", "value2"]);

Add an index to the Table.

Default index is a range 0-N where N is amount of records.

Example
use tabled::Table;

let table = Table::builder(&["Hello", "World", "!"]).index().build();

assert_eq!(
    table.to_string(),
    "+---+-------+\n\
     |   | &str  |\n\
     +---+-------+\n\
     | 0 | Hello |\n\
     +---+-------+\n\
     | 1 | World |\n\
     +---+-------+\n\
     | 2 | !     |\n\
     +---+-------+"
)

Clean removes empty columns and rows.

Example
use tabled::Table;

let mut builder = Table::builder(&["Hello", "World", ""]);
builder.clean();

let table = builder.build();

assert_eq!(
    table.to_string(),
    "+-------+\n\
     | &str  |\n\
     +-------+\n\
     | Hello |\n\
     +-------+\n\
     | World |\n\
     +-------+"
)

Creates a Builder from a built Records

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Extends a collection with the contents of an iterator. Read more
🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Creates a value from an iterator. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.