pub fn render(doc: Box<Doc>, tab: usize, width: usize) -> StringExpand description
Renders a compiled document to a formatted string
This is the final step in the pretty printing process. It takes a compiled Doc structure
and produces the final formatted text output according to the specified formatting parameters.
The rendering process is deterministic and can be called multiple times with different
parameters without recompilation.
The renderer implements sophisticated line breaking algorithms that respect the layout decisions made during compilation while applying the final formatting constraints.
§Arguments
doc- The compiled document to render. Must be aDocproduced by one of the compilation functions (compile(),compile_safe(), orcompile_safe_with_depth()).tab- Number of spaces per indentation level. Common values:- 2: Compact style, common in web development
- 4: Standard style, widely used in many languages
- 8: Traditional tab width, used in system programming
width- Maximum line width for line breaking decisions. The renderer will attempt to keep lines within this limit while respecting the layout structure. Common values:- 40-60: Narrow columns for documentation or mobile display
- 80: Traditional terminal width, widely used standard
- 100-120: Modern wide displays, good for code review
- Unlimited: Use very large values (e.g., 10000) to disable line wrapping
§Returns
A String containing the formatted text output with appropriate line breaks,
indentation, and spacing applied according to the layout structure and formatting parameters.
§Performance Notes
- Time: O(n) where n is the size of the final output text
- Memory: O(n) for the output string allocation
- Reusability: Same document can be rendered multiple times with different parameters
- No Side Effects: Pure function with no global state or side effects
§Formatting Behavior
The renderer applies several formatting rules:
- Indentation: Applied according to
nestandpackconstructs in the original layout - Line Breaking: Respects
grp,seq, and composition breaking rules from compilation - Spacing: Handles padding and spacing between text elements
- Width Constraints: Attempts to fit content within the specified
widthwhen possible
§Examples
§Basic Text Rendering
use typeset::{compile, render, text};
let layout = text("Hello, world!".to_string());
let doc = compile(layout);
// Render with standard settings
let output = render(doc, 4, 80);
assert_eq!(output, "Hello, world!");§Comparing Different Tab Widths
use typeset::{compile, render, text, nest};
let layout = nest(text("indented content".to_string()));
let doc = compile(layout);
// Compare different indentation widths
let narrow = render(doc.clone(), 2, 80);
let standard = render(doc.clone(), 4, 80);
let wide = render(doc, 8, 80);
// Each will have different indentation spacing§Width Constraint Comparison
use typeset::{compile, render, text, comp};
let layout = comp(
text("This is a long line that might need".to_string()),
text("to break depending on width constraints".to_string()),
true, false
);
let doc = compile(layout);
// Narrow width forces breaking
let narrow = render(doc.clone(), 2, 40);
// Wide width allows single line
let wide = render(doc, 2, 120);
println!("Narrow (40 chars):\n{}", narrow);
println!("Wide (120 chars):\n{}", wide);§Complex Document Rendering
use typeset::{compile, render, text, comp, nest, grp};
// Build a function definition layout
let function_layout = grp(comp(
text("function".to_string()),
nest(comp(
text("calculateArea(width, height)".to_string()),
comp(
text("{".to_string()),
nest(comp(
text("const area = width * height;".to_string()),
text("return area;".to_string()),
true, false
)),
false, false
),
false, false
)),
true, false
));
let doc = compile(function_layout);
// Render with different formatting styles
let compact = render(doc.clone(), 2, 60);
let spacious = render(doc, 4, 100);
println!("Compact style:\n{}", compact);
println!("Spacious style:\n{}", spacious);§Batch Rendering with Different Parameters
use typeset::{compile, render, text, comp, nest};
let layout = nest(comp(
text("if condition {".to_string()),
nest(text("do_something();".to_string())),
true, false
));
let doc = compile(layout);
// Render for different contexts
let configurations = [
("Mobile", 2, 40),
("Desktop", 4, 80),
("Print", 4, 120),
];
for (name, tab, width) in configurations {
let output = render(doc.clone(), tab, width);
println!("{} format:\n{}\n", name, output);
}§Unlimited Width Rendering
use typeset::{compile, render, text, comp};
let layout = comp(
text("This is a very long line".to_string()),
text("that would normally break".to_string()),
true, false
);
let doc = compile(layout);
// Disable line wrapping with very large width
let single_line = render(doc, 4, 10000);
// Will keep everything on one line if the layout allows it§See Also
compile()- Fast compilation of layouts to documentscompile_safe()- Safe compilation with error handlingDoc- The compiled document type that this function renders- Layout constructors - Functions for building layouts