Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have some multiline strings with intended whitespace. For some of those, some whitespace is removed:

const WORKING: &str = "
┌─┬┐
│ ││
╞═╪╡
│ ││
├─┼┤
├─┼┤
│ ││
└─┴┘
";

const NON_WORKING: &str = "
  ? 
  │ 
?─┼?
  │ 
?─┼?
?─┼?
  │ 
  ? 
";

pub fn main() {
    println!("{}", WORKING);
    println!("{}", NON_WORKING);
}

It removes some whitespace at the beginning of the line in the non-working one. Printing:

? 
  │ 
?─┼?
  │ 
?─┼?
?─┼?
  │ 
  ? 

I think it has to deal with the use of but I don't know how to solve it without starting the line after the "

Playground

question from:https://stackoverflow.com/questions/65601049/why-do-multiline-strings-skip-intended-whitespace-at-the-beginning

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
413 views
Welcome To Ask or Share your Answers For Others

1 Answer

I haven't tried it myself, but I've heard the indoc crate is intended to help write multiline string literals containing indentation. It removes leading whitespace equally from all lines, rather than 's behavior of removing leading whitespace from each line independently.

Using indoc

use indoc::indoc;

fn main() {
    let testing = indoc! {"
        def hello():
            print('Hello, world!')

        hello()
    "};
    let expected = "def hello():
    print('Hello, world!')

hello()
";
    assert_eq!(testing, expected);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...