How do I write a multi-line string in Rust?

后端 未结 5 1027
长情又很酷
长情又很酷 2021-02-02 06:06

Is it possible to write something like:

fn main() {
    let my_string: &str = \"Testing for new lines \\
                           might work like this?\";
         


        
5条回答
  •  名媛妹妹
    2021-02-02 06:40

    If you'd like to avoid having newline characters and extra spaces, you can use the concat! macro. It concatenates string literals at compile time.

    let my_string = concat!(
        "Testing for new lines ",
        "might work like this?",
    );
    
    assert_eq!(my_string, "Testing for new lines might work like this?");
    

    The accepted answer with the backslash also removes the extra spaces.

提交回复
热议问题