Skip to main content

join_with

Function join_with 

Source
pub fn join_with(
    layouts: Vec<Box<Layout>>,
    separator: Box<Layout>,
) -> Box<Layout>
Expand description

Joins a collection of layouts with a custom separator.

This is the generic joining function that combines multiple layouts with a specified separator between each element. The separator is padded (has spaces around it when on the same line) in the resulting composition. This function forms the basis for other joining utilities.

§Parameters

  • layouts - Vector of layouts to join together
  • separator - Layout to place between each pair of elements

§Returns

A single layout with elements separated by the separator, or null if empty.

§Examples

use typeset::*;

// Join with custom separator
let items = join_with(vec![
    text_str("apple"),
    text_str("banana"),
    text_str("cherry")
], text_str(" | "));

assert_eq!(format_layout(items, 2, 80), "apple | banana | cherry");
use typeset::*;

// Join with arrow separators for pipelines
let pipeline = join_with(vec![
    text_str("input"),
    text_str("process1"),
    text_str("process2"),
    text_str("output")
], text_str(" -> "));

// Output: "input -> process1 -> process2 -> output"
use typeset::*;

// Complex separators with breaking behavior
let statements = join_with(vec![
    text_str("statement1;"),
    text_str("statement2;"),
    text_str("statement3;")
], newline());

// Each statement on its own line

§Behavior

  • Empty vector returns null
  • Single element vector returns that element unchanged
  • Multiple elements are joined with padded compositions
  • The separator is placed between consecutive elements (N-1 separators for N elements)
  • Both the main composition and separator composition use pad=true, fix=false

§Edge Cases

use typeset::*;

// Empty vector
let empty = join_with(vec![], comma());
assert_eq!(format_layout(empty, 2, 80), "");

// Single element
let single = join_with(vec![text_str("alone")], comma());
assert_eq!(format_layout(single, 2, 80), "alone");

§See Also