#string #multi-line #utilities

dev trim-margin

A utility crate to help with layouting multi-line strings by detecting their margin

1 unstable release

Uses old Rust 2015

0.1.0 Jan 13, 2018

#235 in Value formatting

Download history 852/week @ 2023-12-07 1054/week @ 2023-12-14 845/week @ 2023-12-21 1278/week @ 2023-12-28 2807/week @ 2024-01-04 2634/week @ 2024-01-11 3136/week @ 2024-01-18 2802/week @ 2024-01-25 2281/week @ 2024-02-01 3208/week @ 2024-02-08 4579/week @ 2024-02-15 3882/week @ 2024-02-22 3397/week @ 2024-02-29 3300/week @ 2024-03-07 2527/week @ 2024-03-14 4287/week @ 2024-03-21

14,047 downloads per month
Used in 5 crates

Apache-2.0

10KB
71 lines

trim-margin: easy layouting of multi-line strings

Build Status Crates.io

This crate is intended to ease the use of multi-line strings in Rust. When embedding strings with multiple lines in Rust all whitespaces, tabs, etc. are preserved even if they are just used for layouting one's code nicely.

fn main() {
    println!("-----------------------");
    let misrepresented_multiline_string = "
        This is string
        spans over multiple lines,
        but its rendering preserves all whitespaces.

        Which is not what we usually intend in this case.
    ";
    println!("{}", misrepresented_multiline_string);
    println!("-----------------------");

    println!("-----------------------");
    let correctly_layouted_string = "For displaying
the a multiline strin properly
it would need to be layouted
like this.

Which is not very nice.";
    println!("{}", correctly_layouted_string);
    println!("-----------------------");
}

The trim-margin crate supports you with proper layouting. By introducing a margin in the multi-line string the trim_margin method can filter out unwanted whitespaces and blank lines.

extern crate trim_margin;
use trim_margin::MarginTrimmable;

fn main() {
    let multiline_string_with_margin = "
        |This string has a margin
        |indicated by the '|' character.
        |
        |The following method call will remove ...
        | * a blank first/last line
        | * blanks before the margin prefix
        | * the margin prefix itself
    ".trim_margin().unwrap();
    println!("{}", multiline_string_with_margin);
}

No runtime deps