Struct cli_tables::Table
source · pub struct Table { /* private fields */ }Implementations§
source§impl Table
impl Table
pub fn new() -> Self
sourcepub fn push_row(&mut self, new_record: &Vec<&str>) -> Result<(), PushError>
pub fn push_row(&mut self, new_record: &Vec<&str>) -> Result<(), PushError>
Description
Pushes a new row/record to the table.
Arguments
new_record- An immutable reference to a vector of string slices representing the new record. In Rust, the &str type represents a string slice. It is an immutable reference to a string data stored elsewhere, typically UTF-8 encoded. String slices are commonly used to efficiently work with string data without taking ownership of it.
Errors
Returns an error if the number of fields is invalid.
Examples
use cli_tables::{Table, PushError};
let mut table = Table::new(); // creates a new table
let header = vec!["Id", "Title", "Series", "Author"]; // add rows using a vector of string slices
assert_eq!(table.push_row(&header), Ok(())); // handle errors
let book = vec!["0", "Sword of Destiny", "The Witcher Series", "Andrzej Sapkowski"];
assert_eq!(table.push_row(&book), Ok(()));
let invalid_record = vec!["1", "The Last Wish", "The Witcher Series"];
assert_eq!(
table.push_row(&invalid_record),
Err(PushError {
message: "Invalid number of fields in record. Found 3, but expected 4.".to_string()
})
);pub fn to_string(&self) -> String
Trait Implementations§
Auto Trait Implementations§
impl RefUnwindSafe for Table
impl Send for Table
impl Sync for Table
impl Unpin for Table
impl UnwindSafe for Table
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more