How to concatenate static strings in Rust

前端 未结 2 979
深忆病人
深忆病人 2020-12-20 11:34

I\'m trying to concatenate static strings and string literals to build another static string. The following is the best I could come up with, but it doesn\'t work:



        
2条回答
  •  天涯浪人
    2020-12-20 12:03

    The compiler error is

    error: expected a literal

    A literal is anything you type directly like "hello" or 5. The moment you start working with constants, you are not using literals anymore, but identifiers. So right now the best you can do is

    const VERSION_STRING: &'static str =
        concat!("my program v", env!("CARGO_PKG_VERSION"));
    

    Since the env! macro expands to a literal, you can use it inside concat!.

提交回复
热议问题