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

后端 未结 5 1028
长情又很酷
长情又很酷 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:47

    Every line is a multiline in Rust.

    But if you have indents in your text like:

    fn my_func() {
        const MY_CONST: &str = "\
        Hi!
        This is a multiline text!
        ";
    }
    

    But you will get unnecessary spaces. To remove them you can use indoc! macros from indoc crate to remove all indents: https://github.com/dtolnay/indoc

提交回复
热议问题