提问
rust 字符串拼接方式
回答
- format!("{} {}", s1, s2);
fn main() {
let s1 = "Hello";
let s2 = "World";
// Using format! macro for concatenation
let result = format!("{} {}", s1, s2);
println!("{}", result); // Output: "Hello World"
}
- .push_str(s2);
fn main() {
let s1 = "Hello";
let s2 = "World";
// Using push_str method for concatenation
let mut result = s1.to_string();
result.push_str(" ");
result.push_str(s2);
println!("{}", result); // Output: "Hello World"
}
fn main() {
let s1 = "Hello";
let s2 = "World";
// You need to convert &str to String before concatenating
let result = s1.to_string() + " " + s2;
println!("{}", result); // Output: "Hello World"
}
标签:s2,s1,拼接,let,result,字符串,World,Hello,rust
From: https://www.cnblogs.com/wuhailong/p/18280027