[−][src]Macro text_grid::cell
Creates a Cell
using interpolation of runtime expressions.
Use the format!
syntax to create Cell
. See std::fmt
for more information.
Examples
use text_grid::*; struct RowData { a: f64, b: f64, } impl RowSource for RowData { fn fmt_row<'a>(w: &mut impl RowWrite<Source=&'a Self>) { w.column("a", |s| cell!("{:.2}", s.a).right()); w.column("b", |s| cell!("{:.3}", s.b).right()); } } let mut g = Grid::new(); g.push_row(&RowData { a: 1.10, b: 1.11 }); g.push_row(&RowData { a: 1.00, b: 0.10 }); print!("{}", g);
Output:
a | b |
------|-------|
1.10 | 1.110 |
1.00 | 0.100 |
Arguments ownership
This macro consumes the variable used in the argument.
ⓘThis example is not tested
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(|w| write!(w, "{}", &s)); let cell_b = cell_by(|w| write!(w, "{}", &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)); // Retrun value owns formatted string. // Therefore, the returned value can move anywhere.