Skip to main content

pad

Function pad 

Source
pub fn pad(left: Box<Layout>, right: Box<Layout>) -> Box<Layout>
Expand description

Creates a padded composition between two layouts.

This convenience function creates a composition with padding enabled and fixing disabled. When the layouts fit on the same line, a space is inserted between them. When they don’t fit, a line break is inserted instead. This is equivalent to comp(left, right, true, false).

§Parameters

  • left - The left layout in the composition
  • right - The right layout in the composition

§Returns

A padded, breakable composition of the two layouts.

§Examples

use typeset::*;

// Simple padded composition
let spaced = pad(
    text_str("Hello"),
    text_str("world")
);

assert_eq!(format_layout(spaced, 2, 80), "Hello world");
use typeset::*;

// Breaking behavior when narrow
let breakable = pad(
    text_str("very_long_identifier"),
    text_str("another_long_identifier")
);

// Wide: "very_long_identifier another_long_identifier"
// Narrow: "very_long_identifier\nanother_long_identifier"
use typeset::*;

// Chain multiple padded compositions
let chain = pad(
    text_str("first"),
    pad(
        text_str("second"),
        text_str("third")
    )
);

// Output: "first second third" (when it fits)

§Behavior

  • Padding: true - Adds a space when layouts are on the same line
  • Fixing: false - Allows breaking when width constraints require it
  • Equivalent to the most common composition pattern
  • Breaking decisions are made based on available width

§See Also

  • unpad - Composition without spaces
  • fix_pad - Padded composition that never breaks
  • comp - The underlying composition function
  • [join_with_spaces] - For joining multiple layouts with padding