如何连接字符串? [英] How do I concatenate strings?
问题描述
如何连接以下类型的组合:
How do I concatenate the following combinations of types:
str
和str
String
和str
String
和String
str
andstr
String
andstr
String
andString
推荐答案
连接字符串时,需要分配内存来存储结果.最容易开始的是 String
和 &str
:
When you concatenate strings, you need to allocate memory to store the result. The easiest to start with is String
and &str
:
fn main() {
let mut owned_string: String = "hello ".to_owned();
let borrowed_string: &str = "world";
owned_string.push_str(borrowed_string);
println!("{}", owned_string);
}
这里,我们有一个可以改变的自有字符串.这是有效的,因为它可能允许我们重用内存分配.String
和 String
也有类似的情况,如 &String
可以取消引用为 &str
.
Here, we have an owned string that we can mutate. This is efficient as it potentially allows us to reuse the memory allocation. There's a similar case for String
and String
, as &String
can be dereferenced as &str
.
fn main() {
let mut owned_string: String = "hello ".to_owned();
let another_owned_string: String = "world".to_owned();
owned_string.push_str(&another_owned_string);
println!("{}", owned_string);
}
此后,another_owned_string
保持不变(注意没有 mut
限定符).还有另一个变体使用 String
但不要求它是可变的.这是一个 Add
trait 的实现,将 String
作为左侧,&str
作为右侧侧面:
After this, another_owned_string
is untouched (note no mut
qualifier). There's another variant that consumes the String
but doesn't require it to be mutable. This is an implementation of the Add
trait that takes a String
as the left-hand side and a &str
as the right-hand side:
fn main() {
let owned_string: String = "hello ".to_owned();
let borrowed_string: &str = "world";
let new_owned_string = owned_string + borrowed_string;
println!("{}", new_owned_string);
}
请注意,在调用 +
后,owned_string
将不再可访问.
Note that owned_string
is no longer accessible after the call to +
.
如果我们想产生一个新的字符串,同时保持两个不变怎么办?最简单的方法是使用 format!
:
What if we wanted to produce a new string, leaving both untouched? The simplest way is to use format!
:
fn main() {
let borrowed_string: &str = "hello ";
let another_borrowed_string: &str = "world";
let together = format!("{}{}", borrowed_string, another_borrowed_string);
// After https://rust-lang.github.io/rfcs/2795-format-args-implicit-identifiers.html
// let together = format!("{borrowed_string}{another_borrowed_string}");
println!("{}", together);
}
请注意,两个输入变量都是不可变的,因此我们知道它们不会被触及.如果我们想对 String
的任意组合做同样的事情,我们可以使用 String
也可以格式化的事实:
Note that both input variables are immutable, so we know that they aren't touched. If we wanted to do the same thing for any combination of String
, we can use the fact that String
also can be formatted:
fn main() {
let owned_string: String = "hello ".to_owned();
let another_owned_string: String = "world".to_owned();
let together = format!("{}{}", owned_string, another_owned_string);
// After https://rust-lang.github.io/rfcs/2795-format-args-implicit-identifiers.html
// let together = format!("{owned_string}{another_owned_string}");
println!("{}", together);
}
您不必使用格式!
.您可以克隆一个字符串并附加另一个字符串到新字符串:
You don't have to use format!
though. You can clone one string and append the other string to the new string:
fn main() {
let owned_string: String = "hello ".to_owned();
let borrowed_string: &str = "world";
let together = owned_string.clone() + borrowed_string;
println!("{}", together);
}
注意 - 我所做的所有类型规范都是多余的 - 编译器可以推断所有在这里起作用的类型.我添加它们只是为了让 Rust 的新手清楚,因为我希望这个问题在那个群体中很受欢迎!
Note - all of the type specification I did is redundant - the compiler can infer all the types in play here. I added them simply to be clear to people new to Rust, as I expect this question to be popular with that group!
这篇关于如何连接字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!