Skip to main content

brackets

Function brackets 

Source
pub fn brackets(layout: Box<Layout>) -> Box<Layout>
Expand description

Wraps a layout in square brackets.

This convenience function surrounds the given layout with square brackets, using unpadded compositions to ensure no extra spaces are added. This is commonly used for array literals, indexing operations, attribute syntax, and other bracket-delimited constructs.

§Parameters

  • layout - The layout to wrap in square brackets

§Returns

A layout wrapped in square brackets: [content]

§Examples

use typeset::*;

// Array literal
let array = brackets(
    join_with_commas(vec![
        text_str("1"),
        text_str("2"),
        text_str("3")
    ])
);

assert_eq!(format_layout(array, 2, 80), "[1, 2, 3]");
use typeset::*;

// Array indexing
let indexing = comp(
    text_str("array"),
    brackets(text_str("index")),
    false, false
);

assert_eq!(format_layout(indexing, 2, 80), "array[index]");
use typeset::*;

// Multi-line array with grouping
let long_array = brackets(
    grp(nest(join_with_commas(vec![
        text_str("very_long_element_name_1"),
        text_str("very_long_element_name_2"),
        text_str("very_long_element_name_3")
    ])))
);

// When it fits: "[very_long_element_name_1, very_long_element_name_2, very_long_element_name_3]"
// When it breaks:
// [
//   very_long_element_name_1,
//   very_long_element_name_2,
//   very_long_element_name_3
// ]

§Behavior

  • Uses unpadded compositions (pad=false) to avoid spaces around brackets
  • The wrapped content can break internally based on its own structure
  • Brackets themselves never break apart from their content
  • No additional spacing is introduced

§See Also

  • parens - For wrapping in parentheses ()
  • braces - For wrapping in curly braces {}
  • comp - The underlying composition constructor used