如何连接以下类型的组合:
str
和str
String
和str
String
和String
str
和 str
String
和 str
String
和 String
String
和&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
。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
作为右操作数: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
将不再可访问。format!
函数。fn main() {
let borrowed_string: &str = "hello ";
let another_borrowed_string: &str = "world";
let together = format!("{borrowed_string}{another_borrowed_string}");
println!("{}", together);
}
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}");
println!("{}", together);
}
format!
。你可以克隆一个字符串,然后将另一个字符串附加到新字符串上。fn main() {
let owned_string: String = "hello ".to_owned();
let borrowed_string: &str = "world";
let together = owned_string.clone() + borrowed_string;
println!("{together}");
}
.to_owned()
和 .to_string()
的差异已经得到修复。当在 &str
上调用时,它们现在具有相同的性能。相关提交:https://github.com/rust-lang/rust/pull/32586/files - chadString
,但是你取一个引用 (&String
),它可以被强制转换为 &str
。我将整个路径 String
-> &String
-> &str
都写出来了,因为初学者可能甚至不知道你可以取一个 String
的引用。 :-) - Shepmaster要将多个字符串连接成一个字符串,用另一个字符分隔,有几种方法。
我看到过最好的方法是使用数组上的 join
方法:
fn main() {
let a = "Hello";
let b = "world";
let result = [a, b].join("\n");
print!("{}", result);
}
根据您的使用情况,您可能更喜欢拥有更多的控制:
fn main() {
let a = "Hello";
let b = "world";
let result = format!("{}\n{}", a, b);
print!("{}", result);
}
我见过一些更多的手动方法,有些避免了这里或那里的一两个分配。为了可读性,我认为上述两种方式已经足够。
join
实际上是附加在SliceContactExt
特质上的。该特质被标记为不稳定,但其方法是稳定的,而且包含在预导入模块中,因此默认情况下可以在任何地方使用。团队似乎很清楚该特质不需要存在,并且我想未来会有改变。 - Simon Whiteheadstr
,使用join
比s1.to_owned().push_str(s2)
更高效,因为它避免了第二次分配内存。 - Ibraheem AhmedRust中有多种可用的方法来连接字符串。
concat!()
):fn main() {
println!("{}", concat!("a", "b"))
}
上面代码的输出是:
ab
push_str()
和+
运算符):fn main() {
let mut _a = "a".to_string();
let _b = "b".to_string();
let _c = "c".to_string();
_a.push_str(&_b);
println!("{}", _a);
println!("{}", _a + &_c);
}
上面代码的输出为:
ab
abc
format!()
函数):fn main() {
let mut _a = "a".to_string();
let _b = "b".to_string();
let _c = format!("{}{}", _a, _b);
println!("{}", _c);
}
以上代码的输出为:
ab
请查看并在Rust playground上进行实验。
concat
只能用于字面量,因此并不是非常有用。 - Abhijit Sarkar更新:截至2021年12月28日,这个功能在Rust 1.58 Beta 中已经可用。您不再需要使用 Rust Nightly 构建来进行字符串插值。(为了保存答案的完整性,其余部分未作更改)
RFC 2795于2019年10月27日发布:
建议支持隐式参数,以实现许多人所知道的“字符串插值”——一种在字符串中嵌入参数以进行拼接的方式。
RFC:https://rust-lang.github.io/rfcs/2795-format-args-implicit-identifiers.html
最新问题状态可以在此处找到: https://github.com/rust-lang/rust/issues/67984
在撰写本文时(2020年9月24日),我认为这个功能应该可以在 Rust Nightly 构建中使用。
这将允许您通过以下简写进行拼接:
format_args!("hello {person}")
它等同于这个:
format_args!("hello {person}", person=person)
还有一个名为"ifmt"的板条箱,它提供了自己的字符串插值方式:
将两个String
连接起来:
fn concat_string(a: String, b: String) -> String {
a + &b
}
连接两个 &str
:
fn concat_str(a: &str, b: &str) -> String {
a.to_string() + b
}
format!("{a}{b}{c}")
。这与format!("{}{}{}", a, b, c)
基本相同,但稍微更短一些,并且(可以说)更易于阅读。这些变量可以是String
,&str
(对于其他非字符串类型也是如此)。结果是一个String
。
有关更多信息,请参见此处。fn main() {
let a = String::from("Name");
let b = "Pkgamer";
println!("{}",a+b)
}
str
和&str
是不同的类型,99% 的情况下,您只需要关心&str
。有其他问题详细说明它们之间的差异。 - Shepmaster