Multi-line string in Rust with preserved leading whitespace
Asked Answered
A

3

21

In some languages it is possible to write something of this sort:

val some_string =
  """First line.
    | Second line, with leading space."""

That is, a multi-line string where all leading space is removed up to a point, but no further. This can be mimicked in Rust by writing:

let some_string = 
    "First line.\n \
     Second line, with leading space.";

However, this loses the benefit of looking closer to the actual output. Is there a way in Rust of writing something like the example pseudocode, preserving (some) leading whitespace?

Adamok answered 25/10, 2015 at 20:56 Comment(0)
M
12

No it's not possible (v1.3 and probably for a long time).

However, usually multi-line string literals that need to be human-readable are some sort of constant descriptions, like the usage string for a CLI program. You often see those things indented like this:

const USAGE: &'static str = "
Naval Fate.

Usage:
  ...
";

Which is ok I guess. If you have a lot of those strings or a really big one, you could use include_str!.

Maidservant answered 25/10, 2015 at 21:37 Comment(0)
T
27

It is not supported by the language as of Rust 1.7 but Indoc is a procedural macro that does what you want. It stands for "indented document." It provides a macro called indoc!() that takes a multiline string literal and un-indents it so the leftmost non-space character is in the first column.

let some_string = indoc! {"
    First line.
     Second line, with leading space."
};

It works for raw string literals as well.

let some_string = indoc! {r#"
    First line.
     Second line, with leading space."#
};

The result in both cases is "First line\n Second line, with leading space."

Thornburg answered 19/3, 2016 at 11:43 Comment(3)
It's expected on Stack Overflow that you declare affiliation when you are promoting a tool or library that you have created.Faunus
@Faunus Why do you say "it is expected"? Expected by who and why? Do you have references to Meta discussions about it? Here's my take: while I don't think it hurts to self-identify, I don't think "it is expected" or even part of the current guidelines that an author self-identify when linking to an open-source project they have authored or contributed. To form this assessment, I've reviewed various Meta StackExchange posts on the topic of linking to external resources. This one seems to be the most official: meta.stackexchange.com/help/promotion -- and it is silent on the matter.Geomancer
For context, per en.wikipedia.org/wiki/Journalism_ethics_and_standards , "Independence and objectivity: journalists should avoid topics in which they have a financial or personal interest that would provide them a particular benefit in the subject matter, as that interest may introduce bias into their reporting, or give the impression of such bias. In cases where a journalist may have a specific financial or personal interest, the interest should be disclosed;" -- I would like to raise the question: what are the similarities and differences to the present context (StackOverflow)?Geomancer
M
12

No it's not possible (v1.3 and probably for a long time).

However, usually multi-line string literals that need to be human-readable are some sort of constant descriptions, like the usage string for a CLI program. You often see those things indented like this:

const USAGE: &'static str = "
Naval Fate.

Usage:
  ...
";

Which is ok I guess. If you have a lot of those strings or a really big one, you could use include_str!.

Maidservant answered 25/10, 2015 at 21:37 Comment(0)
N
5

You can start the line you want to indent with a ASCII quoted space \x20 or an Unicode quoted space \u{20}.

let some_string = 
    "First line.\n\
     \x20Second line, with leading space.\n\
     \u{20}Third line, with leading space.";

You just need to quote the first space.

let some_string = 
    "First line.\n\
     \x20 Second line, with two leading spaces.\n\
     \u{20} Third line, with two leading spaces.";
Noellenoellyn answered 17/7, 2020 at 10:17 Comment(1)
Will this work if you want 0 leading spaces before each line?Itself

© 2022 - 2024 — McMap. All rights reserved.