pub fn as_table<T: MarkdownTableRow>(table: &[T]) -> StringExpand description
Formats the input as a markdown-style table.
Takes a slice of items that implement MarkdownTableRow and returns a formatted
markdown table as a string. If the input slice is empty, returns an empty string.
§Column Width
Column widths are automatically calculated based on the maximum width of content in each column, including the header.
§Special Characters
Pipe characters (|) in data are automatically escaped with a backslash (\|)
to prevent breaking the table format.
§Example
use markdown_tables::{MarkdownTableRow, as_table};
struct Task {
id: u32,
description: String,
completed: bool,
}
impl MarkdownTableRow for Task {
fn column_names() -> Vec<&'static str> {
vec!["ID", "Description", "Status"]
}
fn column_values(&self) -> Vec<String> {
vec![
self.id.to_string(),
self.description.clone(),
if self.completed { "✓" } else { "✗" }.to_string(),
]
}
}
let tasks = vec![
Task { id: 1, description: "Write documentation".to_string(), completed: true },
Task { id: 2, description: "Add tests".to_string(), completed: true },
Task { id: 3, description: "Review PR | Deploy".to_string(), completed: false },
];
let table = as_table(&tasks);
println!("{}", table);
// Output:
// | ID | Description | Status |
// |----|---------------------|--------|
// | 1 | Write documentation | ✓ |
// | 2 | Add tests | ✓ |
// | 3 | Review PR \| Deploy | ✗ |