Skip to main content

indent_first_line

Function indent_first_line 

Source
pub fn indent_first_line<'a>(text: &'a str, prefix: &str) -> Cow<'a, str>
Expand description

Indent only the first line by the given prefix.

This function is useful when you want to indent the first line of a multi-line expression while preserving the relative indentation of subsequent lines.

ยงExamples


assert_eq!(indent_first_line("First line.\nSecond line.\n", "  "),
           "  First line.\nSecond line.\n");

When indenting, trailing whitespace is stripped from the prefix. This means that empty lines remain empty afterwards:


assert_eq!(indent_first_line("\n\n\nSecond line.\n", "  "),
           "\n\n\nSecond line.\n");

Leading and trailing whitespace coming from the text itself is kept unchanged:


assert_eq!(indent_first_line(" \t  Foo   ", "->"), "-> \t  Foo   ");