Vla*_*eev 4 string concatenation rust
我有什么理由不能将字符串文字与字符串变量连接起来吗?以下代码:
fn main() {
let x = ~"abcd";
io::println("Message: " + x);
}
Run Code Online (Sandbox Code Playgroud)
给出了这个错误:
test2.rs:3:16: 3:31 error: binary operation + cannot be applied to type `&'static str`
test2.rs:3 io::println("Message: " + x);
^~~~~~~~~~~~~~~
error: aborting due to previous error
Run Code Online (Sandbox Code Playgroud)
我想这是一个非常基本且非常常见的模式,fmt!在这种情况下的使用只会带来不必要的混乱.
使用最新版本的Rust(0.11),~不推荐使用tilde()运算符.
以下是如何使用0.11版本修复它的示例:
let mut foo = "bar".to_string();
foo = foo + "foo";
Run Code Online (Sandbox Code Playgroud)
默认情况下,字符串文字具有静态生存期,并且无法连接唯一和静态向量.使用唯一的文字字符串有助于:
fn main() {
let x = ~"abcd";
io::println(~"Message: " + x);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5431 次 |
| 最近记录: |