Combining Adjacent String literals like in C

Does anyone know if there are plans for rust language to borrow the way in C that you can combine adjacent str literals. It really makes much cleaner looking source code.

Here's an example of what I'm talking about using standard C/C++ that works and compiles fine:

#include <stdio.h>
void main() {
    printf("Hello world!\n"
           "I really wish Rust could do this\n"
           "I mean it really makes for prettier code!\n");
}

So, I hope that Rust is improved to handle this some day in the near future:

fn main() {
    print!("Hello World!\n"      
           "I really wish rust could do this\n"
           "I mean it really makes for prettier code!\n");
}

This is the closest I can find for how to do it in Rust, but note, that the code indentation is lost. C
combining adjacent string literals allows using indents in code that do not appear in the output.

fn main() {
    print!(r#"Hello World!      
Too bad this is the only way in Rust to do this.
I mean it makes prettier code if indents can be ignored.
"#);
}
``

It's possible using backslashes, see Strings - Rust By Example for several examples.

2 Likes

There's also concat!, which is a bit more verbose but can also be more versatile in some cases.

print!(concat!(
    "Hello World!\n",
    "I really wish rust could do this\n",
    "I mean it really makes for prettier code!\n"
));

To be explicit, the version with backslashes would/could look like this

fn main() {
    print!("Hello World!\n\
            I really wish rust could do this\n\
            I mean it really makes for prettier code!\n");
}

The concat macro is mostly useful for defining other macros.

2 Likes

I was not aware that backslashes were interpolated that way-- to "eat" up the extra white space. That actually achieves the same result with less clutter.

Thank you very much for that! I will be cleaning up some code today using just that.

4 Likes

For long string literals, it can also make sense to just define a top-level const; which is useful because on the top level you don’t have indentation / not having indentation doesn’t look too bad. You can start the first line after a backslash to get a quite readable result, saving the need for the explicit “\n”s even:

const LONG_MESSAGE: &str = "\
Hello World!
I really wish rust could do this
I mean it really makes for prettier code!
";

fn main() {
    print!("{}", LONG_MESSAGE);
}
4 Likes

This wouldn't be an improvement, this is a very dangerous feature. Consider what happens if you declare a dynamically-sized array of string literals on several lines, and you forget a comma.

Rust already supports newlines in string literals, there's a concat!() macro in std, and if you want to write multi-line string literals, there's indoc.

Anyway, feature requests and RFCs are off-topic here.

3 Likes

Coming from a background of many programming languages over many years I have learned, in my short time with Rust, that when I or others think of "improved" they generally mean do something like some other language one is familiar with does.

Turns out it is the wrong way to look at things. It's pretty hard to find such "improvements" that area actually better.

5 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.