cell

Macro cell 

Source
macro_rules! cell {
    ($ ( $ arg : tt ) *) => { ... };
}
Expand description

Create Cell via runtime expression interpolation, as in format!.

Use the format! syntax to create Cell. See std::fmt for more information.

§Examples

use text_grid::*;
struct RowData {
    a: f64,
    b: f64,
}
impl Cells for RowData {
    fn fmt(f: &mut CellsFormatter<Self>) {
        f.column("a", |s| cell!("{:.2}", s.a).right());
        f.column("b", |s| cell!("{:.3}", s.b).right());
    }
}

let rows = [
   RowData { a: 1.10, b: 1.11 },
   RowData { a: 1.00, b: 0.10 },
];
let g = to_grid(rows);

assert_eq!(format!("\n{g}"), r#"
  a   |   b   |
------|-------|
 1.10 | 1.110 |
 1.00 | 0.100 |
"#);

§Arguments ownership

This macro consumes the variable used in the argument.

use text_grid::*;

let s = String::from("ABC");
let cell_a = cell!("{}", &s); // `s` moved into `cell_a` here
let cell_b = cell!("{}", &s); // ERROR : `s` used here after move

To avoid consume variables, use only variables that implements Copy .

use text_grid::*;

let s = String::from("ABC");
let s = &s; // immutable reference implements `Copy`.
let cell_a = cell!("{}", s);
let cell_b = cell!("{}", s); // OK
// Return value owns the reference.
// Therefore, the returned value can not be move out of the lifetime of the reference.

or use cell_by and write!.

use text_grid::*;
use std::fmt::Write;

let s = String::from("ABC");
let cell_a = cell_by(|f| write!(f, "{}", &s));
let cell_b = cell_by(|f| write!(f, "{}", &s));
// Return value owns the reference.
// Therefore, the returned value can not be move out of the lifetime of the reference.

or use cell() and format!.

use text_grid::*;

let s = String::from("ABC");
let cell_a = cell(format!("{}", &s));
let cell_b = cell(format!("{}", &s));
// Return value owns formatted string.
// Therefore, the returned value can move anywhere.