Rust Lang

Rust Multiline String

A multiline string refers to a string literal that can span multiple lines. Multiline strings are a common occurrence in programming. For example, you can use the multiline strings to store a large text such as an extensive SQL query.

In this tutorial, we will learn the various methods that we can use to create multiline strings in Rust.

Let us dive in.

Method 1: Using the Raw String Literals

One of the most common methods of creating a multiline string literal is using the raw string literals that are available in Rust.

A raw string literal is a string literal that begins with the “r#” prefix. We use the raw string literals to create the strings which contain any character, including characters that typically requires us to escape them in a regular string literal.

Consider the following example:

fn main() {
    let long_string = r###"
    This is a multiline string
    that spans multiple lines.
    It can contain any characters,
    including "quotes" and 'apostrophes'.
    "###;

    println!("
{}", long_string);

}

 

The string contents can include any character including double quotes, single quotes, and backslashes without needing the escape sequences.

The raw string literals can also include a delimiter string of any length as long as it is surrounded by Hash characters (#). This can be useful to avoid collisions with the string contents.

Example:

fn main() {
    let long_string = r###"
    This is a multiline string
    that includes the delimiter string "#".
    The delimiter string can be any length.
    "###;

    println!("
{}", long_string);

}

 

Similarly, this should create a multiline string as determined in the raw format.

Method 2: Using the Rust Text_Block Macro

Rust also provides two macros to create the multiline strings in straightforward steps. The first is the text_block macro.

As the name suggests, this macro creates a multiline string in Rust. The macro definition is as follows:

macro_rules! text_block {
    () => { ... };
    ($line:literal) => { ... };
    ($head:literal $($tail:literal)*) => { ... };
}

 

We can use this macro to create a new multiline string as shown in the following example:

fn main() {
    use text_block_macros::text_block;
    let text = text_block! {
        "abc"
        "def"
        "ghi"
    };
    assert_eq!(text, "abc\ndef\nghi");
}

 

NOTE: Add the extern crate text_block_macros to use the “text_block_macros” crate.

Method 3: Using the Rust Text_Block_Fnl! Macro

This macro allows us to create a new multiline string with a newline character. The macro definition is as follows:

macro_rules! text_block_fnl {
    ($($line:literal)*) => { ... };
}

 

Example:

fn main() {
    use text_block_macros::text_block_fnl;
    let text = text_block_fnl! {
        "abc"
        "def"
        "ghi"
    };
    assert_eq!(text, "abc\ndef\nghi\n");
}

 

The given example should setup a new multiline string and add a newline character for each.

Conclusion

You learned how to use the Rust raw strings using the text_block creates and the provided macros to create the multiline strings in Rust.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list